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!
How to run java in VSCODE
How to run java program on Command Prompt/Terminal with Intellij (WINDOWS)?
How to run a Java program via CMD on Windows? - LambdaTest Community
How can I compile and run Java on my mac? do I need to install any program? or can I just use the Terminal application?
Videos
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
Hi everyone,
I am a complete newbie to java and I'm getting overwhelmed with the barrier of entry to even start writing code. I have VSCode and have my java file, but I can't figure out how to run the code so I can see Hello World!
Here is the code:
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" in the terminal window.
System.out.println("Hello, World");
}
}
And here is the error message when I do java HelloWorld :
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
What do I do? I thought I downloaded all the right extensions and everything. Why can't I just write the code and click a button to run it?
Thank youHey guys,
I'm having trouble understanding how to run a program in a terminal or on command prompt with Windows. Can someone give me a detailed explanation on how to run a program on command prompt/terminal?
Thanks!
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.