updated 22 April 2022
-
Without the compiler added to the
Path
, you cannot use Java in the Command Prompt. -
First, install the latest Java SE from:
https://www.oracle.com/technetwork/java/javase/downloads/index.html
-
After that, in your computer, go to
System Properties
, you selectAdvanced System Settings
. Or, you can directly search:Environment Variables
but, it is better to go to
System Properties
inControl Panel
, because sometimes, coming from search, the computer will not allow you to edit the System Variables. -
You should now be at Environment Variables window. At the bottom, you will see System Variables, select
Path
, then clickEdit
. -
You should add there the directory of the Java compiler. Copy and paste that at the last part, but be careful! It contains several information that should not be deleted.
And, when you don't see the last semicolon
;
from the last directory, you should add that. Mine is:;C:\Program Files (x86)\Java\jdk1.8.0_144\bin;
-
To check whether this is successful, open the Command Prompt and type
javac
then hit
Enter
, if the Command Prompt gives you information, then it is successfully added.
-
Change the directory to where you put your source code.
-
But it is better you put your code on the Desktop directory.
-
If the code is on the Desktop, then open CMD, then type
cd Desktop
then hit
Enter
, this will bring you to the Desktop. -
First, you need to compile the Java code in order to run the code.
-
Type
javac
followed by the filename with the filename extension. Example:javac MyFirstProgram.java
then hit
Enter
. -
If this is correct, then it will bring you to the next line.
-
Then, type
java MyFirstProgram
then hit
Enter
, this will run your code. -
Now, you have the class file and the java file. It is ready for creating the Executable JAr File.
Remember that Java is a high-level programming language and the creators of this language put some barrier on direct manipulation of the memory for security reasons, unlike C that has a direct access to memory.
Also, one implication is that Java should be portable to any OS. Hence, there is the Java Runtime Environment that contains the Java Virtual Machine to fulfill such a thing.
With that, creating the standard executable file is somewhat different. Hence, they call it as Executable JAr File. JAr stands for Java Archive.
-
Given you are done with the compilation, you need to create the manifest file. Within the same directory create a text document (.txt), name it as
manifest.txt
so that calling the file will not be hard. Open that and typeMain-Class: MyFirstProgram
hit
Enter
twice then save the changes.You need to hit
Enter
twice. Java will fulfill a metadata within the manifest file too.Here, I am just using the above example. Of course, if the name of the class file is different, then that should be the name instead of
MyFirstProgram
. -
Then after that you are now ready to create the Executable JAr File.
Open the Command Prompt and change the directory again to where you put your code. Then type
jar cfm MyFirstProgram.jar manifest.txt MyFirstProgram.class
then hit
Enter
.If you have multiple class files, then include them too. Again, I'm using the above example for continuity. To test whether this is successful, double click the new Executable JAr File, and if the program runs, you're done! Happy coding!
https://introcs.cs.princeton.edu/java/85application/jar/jar.html
https://www.javatpoint.com/how-to-make-an-executable-jar-file-in-java