How to compile a java project with a terminal/cmd - java

I got tired of using Eclipse because it was too "plain" and "old style". So I moved on to a program called Sublime Text 3 and I used a package named "material theme" which made the program look flat and all nice and stuff.
But I wanted to code Java in that but I don't know how to compile stuff without using an IDE and it was not easy to find out but I found out that you could do it through the terminal/cmd of your operating system.
But what I don't understand is how I could actually use the javac command which means Java Compile and I searched and I didn't find out because I couldn't understand anything that was posted on either YouTube or Stack Overflow because I'm new to this whole terminal/cmd Java Compiling thing.

You're almost there buddy!
Just be sure you have the Java Development Kit (JDK) installed in your system. The JDK provides you the command javac -which you need to compile your .java program files.
The javac command is not that friendly as you think it is. You have to let it know where to find the java file you want to compile.
For example you saved Main.java inside C:\Projects\Java you must compile it this way javac C:\Projects\Java\Main.java.
Then you can run your program this way too java C:\Projects\Java\Main
As mentioned here... You can find a more detailed explanation to the official tutorial.

You need to install java JDK which you can find here
Then you need to right-click on my computer->properties->advanced system settings->Environment variables -> Click on path -> New
Then add the path to the bin folder inside the install folder for JDK.
Then in cmd you can compile with
javac *.java
inside the directory of your code

Install JDK
(Make sure your PATH variable is set up)
Use SublimeText's Build System, which allows you to compile open java documents with the Ctrl + B command.

You did not install JDK. You need to install JDK which has javac command.
Installing JDK on MAC instructions
Installing JDK on Windows instructions
After installing JDK(Java Development Kit), you can run the command javac -version on command line and let it show the version that is installed.

Related

java JDK 1.8.0 Win 11 - system variables

I've just istalled JDK8 on PC Windows 11 Home
Now im trying to run simple default program from NetBeans14
In environmental variables:
PATH : C:\Program Files\Java\jdk1.8.0_202\bin
CLASSPATH : C:\Program Files\Java\jdk1.8.0_202\bin
Project build on hard disk
C:\Projects\Hello\src\main\java\pack\hello
on cmd going tn cd C:\Projects\Hello\src\main\java\pack\hello
javac compiles .java file on .class file
command "java Hello" output: Error: Could not find or load main class Hello
Hello.class is there.
checking commands, all works:
java
javac
javac -version
Please to support, or send some similar problems posts.
All web answers are speaking about setting PATH and CLASSPATH, where in my case it already took place.
Thanks in Advance
I tried to follow your steps. I downloaded and installed the newest JDK 8 on Windows 11, and NetBeans 14.
Then I looked through the Environment Variables, and the only mention of Java was in the system Path variable:
You see, I have no CLASSPATH variable, nor haven't I manually set anything. Everything was installed automatically. I have no explicit path to C:\Program Files\Java\jdk1.8.0_... in my Path variable.
And I created a simple app in NetBeans. It runs from NetBeans successfully. Just do "Run Project" (F6) in the NetBeans interface.
If you need to run it from the command line for some reason, this question may help.
If you need to run it on another computer, I would advise you not to do it with cmd. Compile it into jar, then wrap it into exe (in case of Windows) using launch4J.

Can't specify SDK in Intellij on Linux

I am running IntelliJ IDEA 2017.1.6 Pro on a Linux (Ubuntu) machine and, it will not let me select an SDK for any Java project.
Whenever I start up the program and go to "Create a New Project" Java has no SDKs available. If go to new and try to specify where my JDKs are installed (I have a few) it always fails with the same error:
I've tried specifying the locations:
/usr/lib/jvm/
/usr/lib/jvm/java-11-openjdk-amd64/
/usr/lib/jvm/java-8-openjdk-amd64/
/usr/lib/jvm/openjdk-11/
/usr/lib/jvm/jdk-14.0.2/
/usr/lib/jvm/default-java
/usr/lib/jvm/java-1.11.0-openjdk-amd64
/usr/lib/jvm/java-1.8.0-openjdk-amd64
(these last three are all just symlinks)
I know I have a JDK installed for my default Java version. I can compile and run code via command line:
justin#stephanie:~/temp$ ls
HelloWorld.java
justin#stephanie:~/temp$ javac HelloWorld.java
justin#stephanie:~/temp$ ls
HelloWorld.class HelloWorld.java
justin#stephanie:~/temp$ java HelloWorld
Hello, World!
The output of readlink -f $(which java) is /usr/lib/jvm/java-11-openjdk-amd64/bin/java (same location for javac exectuable).
Everything I've looked at online suggest that I should just be able to select /usr/lib/jvm/java-11-openjdk-amd64 as my Java SDK but it is failing. I feel like I must be misunderstanding something essential here as I don't know why this isn't working.
Any help would be useful, I've never used IntelliJ before (I've always been an Eclipse user) and it is very frustrating I can't even create a project in it after a couple hours of trying to mess around with it.
1. Please try first with a newer version of IntelliJ, preferably the most current one (currently this is 2020.2)
2. I had the same problem (with version 2020.2 on my Kubuntu system) and fixed it by simply downloading and installing the SDK via IntelliJ.
The /usr directory is mounted at /var/run/host.
You should find your jdk under /var/run/host/usr/lib/jvm.

Can we install two versions of Java JDK on Windows?

I have created an executable JAR file developed on Java version 8. The JAR file was opening on double click. But as the Oracle applications support only Java 6, I had to install JRE 6, but then after the JRE 6 installation, my executable JAR file is not opening.
I have set the JDK 8 bin path in Path environment variables. Is there a solution for this problem? Why is the JAR file not opening after two Java versions in the system?
JAR should open even if two versions 6 and 8 of Java are installed in the system.
You are facing a backward compatibility problem. Backwards compatibility means that you can run a Java 6 program on a Java 8 runtime, but not the other way around.
You can run a lower configuration on a higher configuration, not vice-versa
There are several reasons for that:
Bytecode is versioned and the JVM checks if it supports the version it finds in .class files.
Some language constructs cannot be expressed in previous versions of bytecode.
There are new classes and methods in newer JREs which won't work with older ones.
If you really, really want (tip: you don't), you can force the compiler to treat the source as one version of Java and emit bytecode for another, using something like this:
javac -source 1.8 -target 1.6 MyClass.java
You can compile your code to Java 1.6 bytecode using JDK 1.8. Just take care of the following:
-source=1.8 and -target=1.6 compiler options
If you use Maven, consider having two pom.xml files, with an optional parent file.
Source: Can program developed with Java 8 be run on Java 7?
I am not sure if this solution going to work or not.
Try to run command java -version and look if it returns java 6 or 8 path. Also try to give path of JDK 8 as JAVA_HOME variable and add that into path like this path=%JAVA_HOME%/bin and see if it works. If you get the java 6 as java version try to use above method and then install JRE 6
Hi All Thank you for your response. I kept java6 and reinstalled java8 and now forms and jar both are working!.
In the short term,
the answer is yes. Since both JDK files are downloaded as jar fils it will ok to download both jar files. The reason to not opening after two java versions is as #Elliott said: "in the system is Java 6 can't run Java 8 compiled code, you should be getting an error." That's exactly true but the problem is how to use multiple versions of JDK in a single machine.
Then we have to move on to long term,
The tricky thing is to manage these multiple JDKs and IDEs. It’s a piece of cake if I just use Eclipse for compiling my code because the IDE allows me to configure multiple versions of Java runtime. Unfortunately (or fortunately), I have to use the command line/shell to build my code. So, it is important that I have the right version of JDK present in the PATH and other related environment variables (such as JAVA_HOME).
Manually modifying the environment variables every time I want to switch between JDKs, isn’t a happy task. But, thanks to Windows Powershell, I’m able to write a script that can do the heavy lifting for me.
Basically, what you want to achieve is to set the PATH variable to add the Java bin folder and set the JAVA_HOME environment variable and then launch the correct Eclipse IDE. And, I want to do this with a single command. Let’s do it.
Open a Windows Powershell.
I prefer writing custom Windows scripts in my profile file so that it is available to run whenever I open the shell. To edit the profile, run this command: notepad.exe $profile - the $profile is a special variable that points to your profile file.
Write the below script in the profile file and save it.
function myIDE{ $env:Path = “C:vraajavajdk7bin;” $env:JAVA_HOME = “C:vraajavajdk7” C:vraaideeclipseeclipse set-location C:vraaworkspacemyproject play }
function officeIDE{
$env:Path = "C:vraajavajdk6bin;"
$env:JAVA_HOME = "C:vraajavajdk6"
C:officeeclipseeclipse
}
Close and restart the Powershell.
Now you can issue the command myIDE which will set the proper PATH and environment variables and then launch the Eclipse IDE.
As you can see, there are two functions with different configurations. Just call the function name that you want to launch from the Powershell command line (myIDE).
If any issue please put a comment below!

Do I really need Java to compile Java?

I am on a new install of Elementary OS and I am trying to compile OpenJDK 8 from source. I have downloaded everything but when I run ./configure I get the following...
configure: Could not find a valid Book JDK. You might be able to fix
this by running 'apt-get install openjdk-7-jdk'.
But I do not want to install Java 7 so what am I missing?
Yes, you do. The Java compiler is written in Java.
You need java in order to compile your java programs.
You need the javac file in you bin folder within java to successfully compile them.
Hope this helps!

Can't run java through vim

I'm trying to learn java, and since I'm using 'javac' and 'java' all day, I'm using vim as my editor of choice. But I'm having a problem getting this system to work: I can't even get a minimal "Hello World" program to run! Within vim, I can use :!javac % to compile the current file just fine. However, with a file Test.java, any attempt to call :!java Test yields an UnsupportedClasVersionError, even though the same command works fine outside of vim. I'm completely stumped.
I'm using java7 on windows 7 (64bit) with cmd.exe as my shell.
The Java you invoke is an older version than the Javac you invoke.
You most likely have a Java 6 JRE installed (for browser plugins etc) at system level and a Java 7 JDK installed which you've added at the end of your path.
Either tell Javac to produce Java 6 compatible class files (with -target) or put the JDK in the front of your path.

Categories

Resources