Source: javaindos.
Answer from Nicholas Kadaeux on Stack OverflowLet's say your file is in C:\mywork\
Run Command Prompt
C:\> cd \myworkThis makes C:\mywork the current directory.
C:\mywork> dirThis displays the directory contents. You should see filenamehere.java among the files.
C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\binThis tells the system where to find JDK programs.
C:\mywork> javac filenamehere.javaThis runs javac.exe, the compiler. You should see nothing but the next system prompt...
C:\mywork> dirjavac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.
C:\mywork> java filenamehereThis runs the Java interpreter. You should then see your program output.
If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!
Source: javaindos.
Let's say your file is in C:\mywork\
Run Command Prompt
C:\> cd \myworkThis makes C:\mywork the current directory.
C:\mywork> dirThis displays the directory contents. You should see filenamehere.java among the files.
C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\binThis tells the system where to find JDK programs.
C:\mywork> javac filenamehere.javaThis runs javac.exe, the compiler. You should see nothing but the next system prompt...
C:\mywork> dirjavac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.
C:\mywork> java filenamehereThis runs the Java interpreter. You should then see your program output.
If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!
To complete the answer :
The Java File
TheJavaFile.javaCompile the Java File to a *.class file
javac TheJavaFile.java- This will create a
TheJavaFile.classfile
- This will create a
Execution of the Java File
java TheJavaFileCreation of an executable
*.jarfileYou've got two options here -
With an external manifest file :
Create the manifest file say - MANIFEST.mf
The MANIFEST file is nothing but an explicit entry of the Main Class
jar -cvfm TheJavaFile.jar MANIFEST.mf TheJavaFile.class
Executable by Entry Point:
jar -cvfe TheJavaFile.jar <MainClass> TheJavaFile.class
To run the Jar File
java -jar TheJavaFile.jar
Can somebody teach me how to compile Java Programs from the command line?
compilation - How to compile a .java file in Java? - Stack Overflow
How to compile a java project with a terminal/cmd - Stack Overflow
How do I compile and run Java with NeoVim? (for learning Java)
Videos
I have no idea how to compile from the command line. My previous teacher neglected to teach us this, and I only know how to use Eclipse. Alright, so my issue was more about getting the PATH variable and file placement, not so much the actual commands
To compile the file, open your terminal and type
javac filename.java
To run the generated class file, use
java filename
But to do this you need to have the Java JDK installed in your computer. You can install it with the instructions in How do I install Java?.
OpenJDK works best for me. It's simple and I have never faced any problem with it. Just follow these simple steps:
From Terminal install open jdk
sudo apt-get install openjdk-7-jdkWrite a java program and save the file as filename.java
Now to compile use this command from the terminal
javac filename.javaIf everything works well then a new "filename.class" file should be created.
To run your program that you've just compiled type the command below in terminal:
java filename
NOTE
You can use any text editor (like gedit) ,
replace the filename with watever name you want
you need to be on same directory as the "present working directory" (got by running pwd) while running the command from terminal.
You need to set your classpath so that the Java compiler knows where to find the org.eclipse.* classes. You can do that with a command line switch or an environment variable.
Ok, Stephen C I did this job by hand. I used only Notepad++ (I promise)
- Start Notepad++ and create file HelloWorldSWT.java
- Copy example from author
- Save it!
- Open cmd and go to the directory with HelloWorldSWT.java
Run the command
javac HelloWorldSWT.javaOk, go to the Eclipse directory and find the correct jar
swt-3.4.2-win32-win32-x86.jarRun this again
D:\workspaces\spf_workspace\hand-made>javac -cp "D:\Program files\eclipse3_5\plugins\org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar" HelloWorldSWT.java
All process take 2 minutes.
Don't try to run this:
`D:\workspaces\spf_workspace\hand-made>java -cp "D:\Program files\eclipse3_5\plugins\org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar;." HelloWorldSWT`
Note: I add current dir . to classpath too.
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