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

Answer from Raja G on askubuntu.com
๐ŸŒ
Florida State University
cs.fsu.edu โ€บ ~myers โ€บ cop3252 โ€บ howto โ€บ cmdline.html
Compiling on the Command line with Java SDK
The primary Java SDK tools, which can be accessed from a command prompt/shell, are: javac // the compiler java // loader/interpreter Windows versions of these filenames would be:
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
How do I compile a program via terminal on macbook?
You used something other than a plain text editor to create your source code. This stackoverflow question should answer your problem, even though it's a C language question rather than Java, the origin of the problem is the same: https://stackoverflow.com/questions/23669037/error-on-rtf1-ansi-when-compiling-c-program More on reddit.com
๐ŸŒ r/learnjava
9
1
December 22, 2021
cmd - How do I run a Java program from the command line on Windows? - Stack Overflow
Follow comments above about how to compile the file ("javac MyFile.java" then "java MyFile") https://stackoverflow.com/a/33149828/194872 ... After installing, in edits the Windows PATH environment variable and adds the following to the path C:\ProgramData\Oracle\Java\javapath. Within this folder are symbolic links to a handful of java executables but "javac" is NOT one of them so when trying to run "javac" from ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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 - You can go into the directory where you have kept your source code either by following the typical GUI way or from the terminal using the cd command as well. ... Before running our Java code, we need to compile it first. To compile a Java code/program, we get the class file.
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 15inout โ€บ mac-cmd.html
Java and the Mac OS X Terminal
From the Terminal, navigate to ... machine:~/introcs/hello wayne$ Assuming the file, say HelloWorld.java is in the current working directory, type the javac command below to compile it....
๐ŸŒ
FOSS Linux
fosslinux.com โ€บ home โ€บ programming โ€บ compile and run java from the linux cli (2026 developer guide)
Compile and Run Java from the Linux CLI (2026 Developer Guide)
April 29, 2026 - I run Java directly from the command line. This guide skips the generic package managers and shows you how to set up a proper, isolated Java environment that mirrors professional CI/CD pipelines. The command line is the absolute source of truth for your Java code. If it compiles and runs in the terminal, it will run in a Docker container or a production server.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ executing-java-code-from-the-command-line-d6a3c09bb565
Executing Java Code from the Command Line | Medium
March 9, 2025 - Learn how to compile and run Java programs from the command line using javac and java, manage classpaths, and understand how the JVM processes bytecode.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-compile-and-run-a-java-application-from-the-command-line-413959
How to compile and run a Java application from the command line | LabEx
To compile a single Java file, you can use the javac command followed by the name of the Java file. For example, to compile a file named HelloWorld.java, you would run the following command in the terminal:
๐ŸŒ
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
๐ŸŒ
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
The java command is also part of the Java Development Kit (JDK) and is used to execute Java programs. To run the HelloWorld program we compiled earlier, follow these steps: Open a terminal and navigate to the directory where the HelloWorld.class file is located.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ how do i compile a program via terminal on macbook?
r/learnjava on Reddit: How do I compile a program via terminal on macbook?
December 22, 2021 -

SOLVED

Hello All. I'm new to programming and java. I'm trying to compile a program via terminal on a macbook and I keep running into errors. I was wondering what I need to change to fix this issue. First I changed the directory of my terminal to the folder that has the .java program in it. I ran "ls" to verify that my .java program was in that directory and it was. However, when I run javac Planet.java I get 60+ errors. This is just a small trial code nothing elaborate. Further info: I have had no issues compiling this code when using an online compiler called CodingGround found here: https://www.tutorialspoint.com/compile_java_online.php I just seem to have issues when using the terminal.

Here is my code (I removed spaces in case I had invisible characters that were causing me issues):

class Planet{
void revolve(){
System.out.println("Revolve");
}
public static void main(String[] args){
Planet earth = new Planet();
earth.revolve();
}
}

I have also tried making it a "public" class by changing the first line to public class Planet{ and still run into the same issues.

Picture of Errors Here:

https://postimg.cc/qNGHhnPk

https://postimg.cc/QK7RXWgZ

PS: Solution: was solved by thisisjustascreename: The text editor is the problem. Macbook's default TextEdit.app dose not automatically save in plain text. You have open the file and go to Format up at the top and then click on Make Plain Text and after saving it and making sure it is still just a .java file and run it via terminal with the javac command it will work just fine.

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
    
๐ŸŒ
Medium
medium.com โ€บ @vivekgoyal2301 โ€บ how-to-compile-and-run-a-java-class-using-the-terminal-03ce5ac856c5
๐Ÿš€ How to Compile and Run a Java Class Using the Terminal | by Vivekgoyal | Medium
November 21, 2025 - Use the cd command to move into the folder where your Java file is saved. ... If the compilation is successful, a new file named ClassName.class will appear in the same directory.
๐ŸŒ
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 - You can use any Java program to learn how to compile and run a program. ... 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 ...
๐ŸŒ
Medium
alonso-delarte.medium.com โ€บ compiling-java-on-the-command-line-990ec24e6d6f
Compiling Java on the command line | by Alonso Del Arte | Medium
July 3, 2024 - To compile Java source from the command line and run the resulting programs, itโ€™s nice to have the path variable set to the Java Runtime Environment (JRE) and Java Development Kit (JDK), giving us two fewer paths toโ€ฆ
๐ŸŒ
CodingNomads
codingnomads.com โ€บ how-to-run-java-from-command-line
Executing Java applications from the CLI
This class file contains the byte code created by the Java compiler that the JVM will translate into machine code. To run the Java app, you use the java command. ... Note: you do not need to add the .class extension because the java command only works on files with the .class extension. You should see Hello World! printed out in the CLI window. A Java application can be entirely written and run from the CLI.
๐ŸŒ
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 - Open a terminal window. Navigate to the directory where the Java source file is located using the cd command. Compile the Java source file using the javac command.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 780639 โ€บ java โ€บ compiling-Java-file-terminal
I want to compiling Java file using terminal (Beginning Java forum at Coderanch)
March 16, 2024 - This may help: https://coderanch.com/t/769388/java/NoClassDefFoundError-terminal And more in-depth instructions are at https://coderanch.com/wiki/659740/create-Java-program ... In CMD javac <filename.Java> // compiles the java file and generates byte code whereas java <filename.java> directly runs the java program // I don't recommend this, it's better to first compile and then run ( assuming you are in the right file_path )