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. Answer from mastermeister77 on reddit.com
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - So if we simply copy the directory and add the .class file name without the extension ( .class ) later with a period ( . ), then it satisfies the condition for executing any Java code that has packages declared in the source code. The same process is also applicable for the other operating systems as well. I am providing screenshots from a Linux OS here: Running Java codes having packages within a Linux Machine ยท Great job! ๐Ÿ‘ You can now run any Java code/programs directly using a terminal.
๐ŸŒ
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 - The program will now execute and display any output in the terminal. Note that you should use the fully qualified name of the class containing the main method. If you are using an IDE such as Eclipse or IntelliJ IDEA, you can also run the Java program directly from the IDE without needing to use the terminal.
Discussions

macos - How to run a Java Class in Terminal - Stack Overflow
I followed all the instructions on how to run a class in MacOs Terminal, but all I got for an answer in my terminal instead of my program was: Danylo-RIB:~ mac$ java /Users/mac/Documents/workspace/Calculator/bin/Calculator.class Error: Could not find or load main class .Users.mac.Documents... More on stackoverflow.com
๐ŸŒ stackoverflow.com
cmd - How do I run a Java program from the command line on Windows? - Stack Overflow
Why is it different than another environment (I thought JVM was write once, run anywhere)? ... I'm at the command line now, do I need to save my txt files in the same folder as the program for them to be invoked? ... @Elizabeth Turner first you have to make sure that you have installed JRE (Java ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to run a simple Java program using terminal command? - Unix & Linux Stack Exchange
As you can see I'm trying to run a Java program but I need the command which I can write in terminal to run the code. More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
April 2, 2017
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
๐ŸŒ
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. Step 2: Write your Java code in a text editor such as Notepad or any other code editor. Save the file with a .java extension and file type as 'All files' in the desired location. Step 3: Open the command prompt (Windows) or terminal (Mac or Linux).
๐ŸŒ
DEV Community
dev.to โ€บ kirubagaran_k_438a86c991 โ€บ how-to-run-a-java-program-in-linux-terminal-4fo6
How to Run a Java Program in Linux Terminal - DEV Community
August 19, 2025 - First, create a folder to store your Java program. Inside that folder, create a .java file using any text editor . ... Open the terminal. Use the command to move into the folder where your .java file is saved ... To compile, run.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-compile-and-run-a-java-program-in-the-terminal-413960
How to compile and run a Java program in the terminal | LabEx
Additionally, the javac command supports various options and flags that can be used to customize the compilation process, such as setting the source and target Java versions, enabling or disabling certain compiler warnings, and more. You can explore these options by running javac -help in the terminal. Now that you've learned how to compile Java source code, let's move on to the next step: running Java programs in the terminal.
Find elsewhere
Top answer
1 of 13
274

Source: javaindos.

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

Run Command Prompt

CopyC:\> cd \mywork

This makes C:\mywork the current directory.

CopyC:\mywork> dir

This displays the directory contents. You should see filenamehere.java among the files.

CopyC:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

CopyC:\mywork> javac filenamehere.java

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

CopyC:\mywork> dir

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

CopyC:\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
121

To complete the answer :

  1. The Java File

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

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

    Copyjava 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

    Copyjava -jar TheJavaFile.jar
    
๐ŸŒ
Dracula Servers
draculaservers.com โ€บ home โ€บ how to run java programs in linux terminal?
How to Run Java Programs in Linux Terminal? - Dracula Servers Tutorials
June 30, 2024 - Ensure the installation of JDK, compile the Java code into a Java Program, and then run the Java Program through the Linux Terminal.
๐ŸŒ
Wikitechy
wikitechy.com โ€บ technology โ€บ how-to-run-the-java-program-in-command-prompt
How to Execute and Run Java Code from the Terminal
August 8, 2024 - The javac command translates your Java code into bytecode that can be executed by the Java Virtual Machine (JVM). ... Open Command Prompt and navigate to the directory containing your Java file. Compile your Java program using the javac command: javac HelloWorld.java. ... Finally, you can run your Java program using the java command.
๐ŸŒ
It's FOSS
itsfoss.com โ€บ run-java-program-ubuntu
How to Run Java Programs in Ubuntu
January 11, 2023 - You can use Nano editor in terminal or Gedit graphical text editor for writing your Java programs. ... If there is no error, the above command produces no output. When you compile the Java program, it generates a .class file with the class name you used in your program. You have to run this class file.
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 11hello
1.1 Your First Java Program: Hello World
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.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ executing-java-code-from-the-command-line-d6a3c09bb565
Executing Java Code from the Command Line | Medium
March 9, 2025 - Itโ€™s important to omit the .class extension in the command. The java command looks for a class file with the same name as the one specified and attempts to run its main method. If the class contains a valid main method, the program will execute, and you will see the output in the terminal:
๐ŸŒ
FreelancingGig
freelancinggig.com โ€บ home โ€บ how to run java on terminal?
How to Run Java on Terminal? - FreelancingGig Blog - Freelancer Job Tips and Hiring Insights
November 8, 2017 - Now save this file and compile it on a terminal. After compilation of the program, you have to enter javac YourJavaClass.java. Suppose you find any error in your program then correct it. If you want to compile the whole program, then hit โ€˜Enterโ€™. ... To launch applet use the appletviewer. The program appletviewer work on HTML and that will help you to run applet outside of the context of a web browser.
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 15inout โ€บ mac-cmd.html
Java and the Mac OS X Terminal
Try running your program with the command line ยท machine:~/introcs/hello wayne$ java -cp ./ HelloWorld If this works, your classpath is set incorrectly. I get the error "class file has wrong version 50.0, should be 49.0" when I compile from the Terminal. What does this mean? It's probably because DrJava is configured to use Java 6.0 and and your Terminal is configured to use Java 5.0.
๐ŸŒ
Wikihow
wikihow.com โ€บ computers and electronics โ€บ software โ€บ programming โ€บ java โ€บ how to compile and run a java program using command prompt
How to Compile and Run a Java Program Using Command Prompt
September 28, 2025 - ... Windows: Click the Windows Start menu and type CMD. Click the Command Prompt icon. Mac: In Finder, press the Go tab, select Applications, select Utilities, then select Terminal.
๐ŸŒ
LaunchCode
education.launchcode.org โ€บ java-web-dev-curriculum โ€บ intro-and-setup โ€บ java-terminal โ€บ index.html
Java in the Terminal :: Java Web Development
October 1, 2021 - Use terminal commands to find the directory you wish to save your new file in. Use the following commands to create your .java file: touch HelloWorld.java ยท Return to your text editor and open the file you just created. Paste in the HelloWorld code from the textbook and save the document. You should now be able to run the program ...
๐ŸŒ
Code With Arjun
codewitharjun.com โ€บ home โ€บ blogs โ€บ run java using command prompt / terminal
Run Java Using Command Prompt / Terminal - Code With Arjun
March 26, 2025 - Compile and Run Java Program using Command Prompt or Terminal, Notepad using Java commands: javac and java.
๐ŸŒ
FOSS Linux
fosslinux.com โ€บ home โ€บ programming โ€บ compile and run java from the linux cli (2026 developer guide)
How to Compile and Run Java from Command-line in Linux
April 29, 2026 - Suppose you have an older legacy application that absolutely requires Java 11 to run. You can install Java 11 alongside your existing Java 21 environment: ... fosslinux@dev:~$ sdk use java 11.0.22-tem Using java version 11.0.22-tem in this shell. fosslinux@dev:~$ java -version openjdk version "11.0.22" 2024-01-16 ยท When you close the terminal, your system reverts to your default version.