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
Discussions

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
command line - Run java classfile from terminal - Unix & Linux Stack Exchange
Does your project use multiple class files? Do you still get the exception if you add the folder with the somefile.class to your path? Lord Loh. – Lord Loh. 2011-06-20 21:23:35 +00:00 Commented Jun 20, 2011 at 21:23 · @LordLoh Do you mean to my environmental PATH variable? ... If you run java folder/somefile java tries to find a class somefile in ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
Using a text file as input for Java
Hello, im trying to learn how to use sublime text and usually I use a text file as my inputs after compiling my Java program. For example, in terminal (UNIX) I would use “java HelloWorld More on forum.sublimetext.com
🌐 forum.sublimetext.com
0
0
August 13, 2020
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
🌐
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.
🌐
Florida State University
cs.fsu.edu › ~myers › cop3252 › howto › cmdline.html
Compiling on the Command line with Java SDK
Recommend using a Windows-based text-editor to actually edit the files. Wordpad is fine for this purpose. You will probably need to set the "PATH" variable. See below · To run a program from the command line from any directory, you either have to type its full location and name every time (annoying) or set its location in your "PATH". To set the path... You'll need to know where your Java SDK was installed.
🌐
CodingNomads
codingnomads.com › how-to-run-java-from-command-line
Executing Java applications from the CLI
Please follow the steps below to run your first Java app from the CLI. First, open your terminal and ensure you're in the right working directory. For instance: ... Use Vim to create and write a new Java file.
Find elsewhere
🌐
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.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 11hello
1.1 Your First Java Program: Hello World
Execute (or run) it by typing "java MyProgram" in the terminal window. The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named MyProgram.class); the third actually runs the program.
🌐
GitConnected
levelup.gitconnected.com › java-in-the-terminal-8888310a16ae
Java in the Terminal. Learning How to Run Java using Only the… | by Jordan Williams | Level Up Coding
February 2, 2021 - Java in the Terminal Learning How to Run Java using Only the Command Line I don’t know if it’s how I was taught Java but my go-to method of running it is through the terminal. I enjoy the …
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
    
🌐
W3Schools
w3schools.com › java › java_getstarted.asp
Java Getting Started
Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java": ... This will compile your code. If there are no errors in the code, the command prompt will take you to the next line.
🌐
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 - You can execute the program by using the java command followed by the name of the class which contains the main method. The general syntax is java [class_name] The program will now execute and display any output in the terminal.
🌐
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 - This wikiHow article teaches you ... Terminal. At the command prompt, type "cd" followed by the path your Java program is saved to, then press "Enter." Type "javac [filename] and press "Enter" to compile the program....
🌐
Quora
quora.com › How-do-I-run-a-Java-program-in-terminal-Windows
How to run a Java program in terminal Windows - Quora
Answer (1 of 3): Install Java. * Visit the oracle website and then click on download. * Click on Accept License Agreement and download the .exe file of JDK for windows. * Download the file and start the installation process by clicking on the file. * Keep clicking on next and don't alter any...
🌐
OnlineGDB
question.onlinegdb.com › 13881 › how-can-i-run-a-java-file-in-the-terminal
How can I run a Java file in the terminal? - OnlineGDB Q&A
Hi! I want to code a java script in the terminal, but I don't know how. I use the "VIM ... I meant a script written in Java. Your help is appreciated!
🌐
Quora
quora.com › How-do-I-run-a-Java-program-in-Mac-using-a-terminal
How to run a Java program in Mac using a terminal - Quora
Answer (1 of 2): There are two different ways you can run a Java program in mac using terminal.. 1. Writing the program using nano editor in the terminal itself and running it. 2. Creating a java file, saving it with .java extension and then opening terminal to run it. *Make sure you've Java ins...
🌐
JetBrains
jetbrains.com › help › idea › run-java-applications.html
Tutorial: Run a Java application | IntelliJ IDEA Documentation
If the Welcome screen opens, click New Project. Otherwise, go to File | New | Project in the main menu. In the New Project list on the left, select Java. Give the project a name (for example, RunApplication).
🌐
Sublime Forum
forum.sublimetext.com › t › using-a-text-file-as-input-for-java › 53229
Using a text file as input for Java - Technical Support - Sublime Forum
August 13, 2020 - Hello, im trying to learn how to use sublime text and usually I use a text file as my inputs after compiling my Java program. For example, in terminal (UNIX) I would use “java HelloWorld < helloworld.in” where helloworld…
🌐
Quora
quora.com › How-do-I-compile-and-run-a-Java-program-in-a-package-from-terminal-Ubuntu
How to compile and run a Java program in a package from terminal (Ubuntu) - Quora
Open the terminal window and navigate to the directory where you copied the downloaded file. Then type the command: sudo shjdk-9.0.4_linux-x64_bin and press enter. If it asks for the root password, give it and JDK installation will begin. The J... ... B.E in IT from International Institute of Information Technology, Pune (I2IT) (Graduated 2022) · Author has 167 answers and 202.6K answer views · 4y ... Before running any Java ...