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
Top answer
1 of 13
274

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
121

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 - [ Mac OS X · Windows · Linux ] We break the process of programming in Java into three steps: 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.
Discussions

Can somebody teach me how to compile Java Programs from the command line?
Well, first you need to make sure you have Java SDK installed. I'm sure you do. Next you need to make sure the bin folder in your java folder is in your class path. Then from the command line, from in the directory where your file(s) is/are located, you just type javac filename.java and compile it. Then java filename to run it. More on reddit.com
🌐 r/learnprogramming
20
53
August 23, 2013
compilation - How to compile a .java file in Java? - Stack Overflow
In addition to compilation, there ... and Eclipse has tools for that, too. If you really want to build outside of Eclipse, you need to manage a potentially large list of dependencies (such as org.eclipse.swt.widgets), which makes a pure javac unfeasible. You would need to look at Ant or Maven. Also note that you will need the classpath to include dependencies not only for compilation, but also when you run the program... More on stackoverflow.com
🌐 stackoverflow.com
How to compile a java project with a terminal/cmd - Stack Overflow
The JDK provides you the command ... .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 ... More on stackoverflow.com
🌐 stackoverflow.com
How do I compile and run Java with NeoVim? (for learning Java)
With a terminal. For more than compiling few classes, you will probably use a build tool like Maven or Gradle or alike which are perfectly fine in a terminal. What would bug me more is the absence of edition tooling like class navigation, autocompletion, ... Did not try yet NeoVim for Java. More on reddit.com
🌐 r/neovim
37
22
March 14, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › java › compilation-execution-java-program
Compilation and Execution of a Java Program - GeeksforGeeks
October 14, 2025 - The JVM (Java Virtual Machine) ... i.e. "binary" (001001010) and then execute the program. Running a Java program involves two main steps: compilation and execution....
🌐
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: ... Congratulations! You have written and executed your first Java program.
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › unixhelp › debuggingtips_Java.html
Compiling and Running Java Programs
Once you have successfully compiled your Java source code, you can invoke the Java VM to run your application's byte-code: % java &ltclass with main method to run&gt [&ltcommand line args&gt, ...] For example, to run the main method from the Foo class: % java Foo Any command line arguments (arguments to Foo's main method) follow the class name: ... When the Java VM runs your program it searches for application .class files using the paths listed in your CLASSPATH environment variable.
Find elsewhere
🌐
Oracle
oracle.com › java › technical details
Essentials, Part 1, Lesson 1: Compiling & Running a Simple Program
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 and run the program. The Java interpreter is invoked at the command line on Unix and DOS shell operating ...
🌐
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("Start small.
🌐
UCSC
users.soe.ucsc.edu › ~eaugusti › archive › 102-winter16 › misc › howToCompileAndRunFromCommandLine.html
How to compile and run a Java Program from the command line
Enter the following command (note that you can compile single or multiple files by name or wildcards): javac -Xlint:unchecked SourceFile.java Running Java Programs From the Command Line
🌐
Florida State University
cs.fsu.edu › ~myers › cop3252 › howto › cmdline.html
Compiling on the Command line with Java SDK
javac // the compiler java // loader/interpreter Windows versions of these filenames would be: javac.exe java.exe These commands (discussed below) will work in any of the following command-line prompts, as long as the SDK is installed: ... To run a java program, use the java command.
🌐
OnlineGDB
onlinegdb.com › online_java_compiler
Online Java Compiler - online editor
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it.
🌐
Google Play
play.google.com › store › apps › details
Compile Java - Run .java Code - Apps on Google Play
Java Compiler is - Free with no hidden charges! - Ad-Free! - Available for all platforms! Features : 1. Compile & Run - Compile & Run Java Programs 2. Code Editing & Syntax Highlighting - Enables you to edit code and automatically detects and highlights syntax 3.
Rating: 2.7 ​ - ​ 297 votes
🌐
BeginnersBook
beginnersbook.com › 2013 › 05 › first-java-program
How to Compile and Run your First Java Program
You have to compile the program first then you can run it. After setting up the path, compile the program by typing “javac FirstJavaProgram” without quotes in command prompt and hit enter then type “java FirstJavaProgram” without quotes.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - If the compilation process is successful, then we will not get any errors. This will create the class file we need under the same directory. Keep in mind that we run the class file, not the .java file. The same process is applicable for all of the operating systems out there. ... We run the .class file to execute the Java programs.
🌐
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 - Type "javac [filename] and press "Enter" to compile the program. Type "java [filename]" and press "Enter" to run the Java program after it is compiled.
🌐
OneCompiler
onecompiler.com › java
Java Online Compiler
OneCompiler's Online Java Editor helps you write, compile, run and debug Java code online. The code runs on latest JDK & JRE
🌐
Quora
quora.com › How-do-you-compile-a-Java-project-which-consists-of-multiple-Java-files-on-the-command-line-command-prompt-terminal
How to compile a Java project which consists of multiple Java files on the command line (command prompt/terminal) - Quora
Answer: nothing special, just ‘javac [options] ’. You can list as many source files as you wish. You can also use a command file instead, which is handy if you are compiling a huge number of source files. There are a couple other things that can appear on the command line, like the ...