Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

C:\> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

This 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\bin

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the next system prompt...

C:\mywork> dir

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere

This 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!

Answer from Nicholas Kadaeux on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_getstarted.asp
Java Getting Started
This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type "java Main" to run the file:
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ java โ€บ java-tutorial
Getting Started with Java in VS Code
November 3, 2021 - VS Code also provides IntelliSense ... Java Editing. To run and debug Java code, set a breakpoint, then either press F5 on your keyboard or use the Run > Start Debugging menu item....
Discussions

How to run java in VSCODE
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/learnjava
6
5
October 24, 2024
How to run java program on Command Prompt/Terminal with Intellij (WINDOWS)?
For my java courses all of our programs had to be run from command prompt so I understand your need. A lot of prof's want you to be comfortable with the command line. First, you have to navigate to the folder where the java file is located. You can do this using the "cd" command to move around to where you keep your files. Then you compile the program from the java file with the main method. Make sure any class files you need are in same folder. compile: javac MainMethodFile.java If no exceptions, then you launch: java MainMethodFile Make sure to just use the file name not the whole file extension when you launch. Don't use the .java or .class extension. More on reddit.com
๐ŸŒ r/javahelp
3
1
June 6, 2020
How to run a Java program via CMD on Windows? - LambdaTest Community
How can I run a Java program from the command line on Windows? Iโ€™m trying to execute the following Java program from the command line in Windows: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import ... More on community.lambdatest.com
๐ŸŒ community.lambdatest.com
0
March 12, 2025
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?
I am currently taking a class on java and we use jGrasp very easy and simple layout! To use this you will need JRE and JDK . Good Luck! More on reddit.com
๐ŸŒ r/javahelp
15
5
October 15, 2014
Top answer
1 of 13
273

Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

C:\> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

This 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\bin

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the next system prompt...

C:\mywork> dir

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere

This 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!

2 of 13
120

To complete the answer :

  1. The Java File

    TheJavaFile.java
    
  2. Compile the Java File to a *.class file

    javac TheJavaFile.java
    
    • This will create a TheJavaFile.class file
  3. Execution of the Java File

    java TheJavaFile
    
  4. Creation of an executable *.jar file

    • You've got two options here -

      1. 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

      2. Executable by Entry Point:

        • jar -cvfe TheJavaFile.jar <MainClass> TheJavaFile.class
  5. To run the Jar File

    java -jar TheJavaFile.jar
    
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 11hello
1.1 Your First Java Program: Hello World
June 10, 2022 - Create the program by typing it into a text editor and saving it to a file named, say, MyProgram.java. Compile it by typing "javac MyProgram.java" in the terminal window. Execute (or run) it by typing "java MyProgram" in the terminal window.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ online-compiler
Online Java Compiler - Programiz
// Online Java Compiler // Use this editor to write, compile and run your Java code online class Main { public static void main(String[] args) { System.out.println("Try programiz.pro"); } } Output ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-run-java-program
How to Run Java Program? - GeeksforGeeks
July 23, 2025 - To run a Java program, you need to follow these steps: Step 1: Install Java Development Kit (JDK) on your computer. You can download the JDK from the Oracle website or any other reliable source.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ how to run java in vscode
r/learnjava on Reddit: How to run java in VSCODE
October 24, 2024 -

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 you
Top answer
1 of 4
4
That error message means you're either not running from the correct directory or the code isn't compiled. If you want a single button to handle this automatically, and you don't want to stress over installing everything needed to run Java in VS Code, just install the "Extension Pack for Java" from the extensions tab in VS Code. After it finishes installing, it will prompt you to install a version of the JDK (Java Development Kit). Simply choose the latest one (JDK 21). After that, the next task it will ask you to do is create a Java project using the command palette. You can access this by clicking at the top or pressing CTRL+Shift+P and typing: Java: Create Java Project. Then, choose "No build tools" and select the location where you want the code to be saved when prompted. It will set everything up for you and open the project. Once the project is open, go into the "src" folder and open App.java. This file should already contain the "Hello, World!" code. You can then press the "Run Java" button at the top right (it looks like a play button) to run the code. This button should both compile and run the code for you. If you're confused about why the project is structured this way or what "compiling code" means, I recommend taking some time to research Java and how it works before diving into the coding aspect. I hope this helps!
2 of 4
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
Find elsewhere
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ languages โ€บ java
Java in Visual Studio Code
November 3, 2021 - Open the Command Palette (โ‡งโŒ˜P (Windows, Linux Ctrl+Shift+P)) and type "java tips" to select the command. You can use VS Code to read, write, run, and debug Java source file(s) without creating a project. VS Code for Java supports two modes, lightweight and standard.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - We run the .class file to execute the Java programs. For that, we use the command java class_file_name_without_the_extension. Like, as our .class file for this is Main.class, our command will be java Main.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-type-run-Java-code-using-CMD-Window-R
How to type, & run Java code using CMD (Window+R) - Quora
Answer: You can type java program in note pad and compile in cmd. 1.you have to save the java program with .Java extension in some folder. 2.Open cmd change the directory to where the file has been stored.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 651219 โ€บ java โ€บ run-java-program
How to run a java program from anywhere? (Java in General forum at Coderanch)
June 13, 2015 - The location of your shell script should be in your path (e.g. the PATH environment variable in Windows). The script will need the location of your Java application files to be hard coded or you could use another environment variable to point to it. Once all this is in place you can run your Java application from any directory in the system.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1822840 โ€บ how-to-run-java-programs-
How to run java programs ? | Sololearn: Learn to code for FREE!
Hi Faiz Tchekiken , ๐ŸŽ their ... going on create new file -> select Java application -> name your project-> then an field will come up to the screen with default Java class with the name of your project and default code...
๐ŸŒ
Eclipse
help.eclipse.org โ€บ latest โ€บ topic โ€บ org.eclipse.jdt.doc.user โ€บ tasks โ€บ task-launching_java_program.htm
Launching a Java Program
In the Package Explorer, select the Java compilation unit or class file with the main method you want to launch. Press the Run [ ] button in the workbench toolbar or select Run > Run from the workbench menu bar.
๐ŸŒ
Oracle
oracle.com โ€บ java โ€บ technical details
Essentials, Part 1, Lesson 1: Compiling & Running a Simple Program
... Once your program successfully ... VM built in such as Netscape or Internet Explorer. Interpreting and running a Java program means invoking the Java VM byte code interpreter, which converts the Java byte codes to platform-dependent machine codes so your computer can understand ...
๐ŸŒ
Medium
medium.com โ€บ @burakkocakeu โ€บ how-to-run-your-java-program-on-terminal-b4956e7102a8
How to Run your Java Program on Terminal | by Burak KOCAK | Medium
January 29, 2023 - Once you have the JDK installed, you can use the following steps to run a Java program on the terminal: ... Open a terminal window. Navigate to the directory where the Java source file is located using the cd command.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-run-a-java-program
How to run a java program
Compiling and running a java program is very easy after JDK installation. Following are the steps ? Open a command prompt window and go to the directory where you saved the java program (MyFirstJavaProgram.java). Assume it's C:\. Type 'javac MyFirstJavaProgram.java' and press enter to compile your code...
๐ŸŒ
Vbspu
vbspu.ac.in โ€บ e-content โ€บ How-to-Compile-and-Run-your-First-Java-Program.pdf pdf
How to Compile and Run your First Java Program
In this tutorial, we will see how to write, compile and run a java program. I will also ยท cover java syntax, code conventions and several ways to run a java program.
๐ŸŒ
LambdaTest Community
community.lambdatest.com โ€บ general discussions
How to run a Java program via CMD on Windows? - LambdaTest Community
March 12, 2025 - How can I run a Java program from the command line on Windows? Iโ€™m trying to execute the following Java program from the command line in Windows: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class CopyFile { public static void main(String[] args) { InputStream inStream = null; OutputStream outStream = null; try { File afi...