While many programming environments allow programs to be compiled and run, they can also be compiled and run using the command line. Windows and Mac have their own versions of the command line, on Mac OS it's called Terminal. The compilation and run process for Windows and Mac is almost identical.
Method 1 of 2: Compiling and Running
Step 1. Save the program
After creating a Java program using a text editor such as NotePad, save it with the.java extension. The file name, of course, can be anything. In this tutorial, we will use "filename" as the name of the file.
- To save the file as.java, after the file name write.java and select All files in the drop-down menu for selecting extensions.
- Remember where you saved the file.
- If you don't know how to write a Java program, look for additional tutorials on that. However, you can use any Java program to learn how to compile and run programs.
Step 2. Open Command Prompt / Terminal
Command line access is slightly different for Mac and Windows.
click start then type cmd.
To open Command Prompt, press ↵ Enter.
in Finder click on the tab Transition, select Programs, then - Utilities and click on Terminal.
Step 3. Check if Java is installed
At the command prompt, enter
java -version… If java is installed, you will see a message with the version of Java installed.
If not, you need to install Java Development Kit from their website. It can be downloaded for free from the link:
Step 4. Navigate to the desired folder
To change the working directory use the command cd, and then enter the name of the directory.
- For example, if you are currently in the directory
C: \ Users \ Bob \ Projectand want to change it to
C: \ Users \ Bob \ Project \ TitanProject, enter
cd TitanProjectand press ↵ Enter.
- If you enter
dirand press ↵ Enter, you can see a list of files that are in this directory.
Step 5. Compile the program
Once you find yourself in the right directory, you can compile the program - enter in the command line
javac filename.javaand hit enter.
- If your program has any errors or has difficulty compiling, the command line will warn you about it.
- For more help, check out our article on How to Fix Compiler Errors in Java.
Step 6. Run the program
java filenameand press ↵ Enter. Replace "filename" with your filename of course.
After pressing ↵ Enter, your program should start. If you receive an error message or your program does not work, use the troubleshooting method
Method 2 of 2: Troubleshoot errors
Step 1. Set the path
If you're using a simple program that has files in the same directory, you probably don't need to. However, if you are using a more complex program with files in multiple directories, you will need to tell your computer where to look for those files.
at the command prompt, enter
java -versionand press ↵ Enter. Based on the Java version listed on the first line, type at the command prompt
set path =% path%; C: \ Program Files \ Java \ jdk1.5.0_09 \ binand press ↵ Enter. Replace jdk1.5.0_09 the version of Java you have installed.
Enter this command when you are in the directory with your Java program
to make sure you have Java installed, enter the command in the terminal
/ usr / libexec / java_home -v 1.7and press ↵ Enter. Then enter
echo export "JAVA_HOME = \ $ (/ usr / libexec / java_home)" >> ~ /.bash_profileand press ↵ Enter. Then restart the terminal.