How to Compile and Execute Java Using Command Prompt

While many programming environments allow you to compile and execute a program within that program, you can also compile and execute a program using the Command Prompt command prompt.

Compile and execute

How to Compile and Execute Java Using Command Prompt Picture 1How to Compile and Execute Java Using Command Prompt Picture 1

Save the program. After using a text editor, such as NotePad, to create a Java program, we save it with the '.java' extension. About the file name, you can set it arbitrarily. For the purposes of this article, "filename" will be used to represent your filename.

To make sure your file is saved as a '.java' file, don't forget to add '.java' after the filename and select All Files from the drop-down menu.

Note down the location where you save the file on the system.

If you are not sure how to write a Java program, you can find out about it on the Internet. Here, however, you can use any Java program to practice compiling and running the program on the command prompt.

How to Compile and Execute Java Using Command Prompt Picture 2How to Compile and Execute Java Using Command Prompt Picture 2

Open Command Prompt/Terminal. The command line access operation on Mac and Windows operating systems is slightly different.

On Windows: Click ⇱ Home and then type cmd. Press ↵ Enter to open Command Prompt.

On Mac: In the Finder tool, click the Go tab > Applications > Utilities > Terminal.

How to Compile and Execute Java Using Command Prompt Picture 3How to Compile and Execute Java Using Command Prompt Picture 3

Check if the Java runtime environment is installed. Type java -version into the command line. If everything is installed, you should see a response indicating the current Java version on the machine.

If not, you need to install the Java Development Kit from the Java homepage. You can download it for free at this link: http://www.oracle.com/technetwork/java/javase/downloads/index.html.

How to Compile and Execute Java Using Command Prompt Picture 4How to Compile and Execute Java Using Command Prompt Picture 4

Navigate to the correct folder of the program. Use the cd command along with the directory name to change the active directory.

For example, if you are operating at C:/Users/Bob/Project and want to navigate to C:/Users/Bob/Project/TitanProject , type cd TitanProject and press ↵ Enter.

You can see the list of current directories by typing dir and then ↵ Enter.

How to Compile and Execute Java Using Command Prompt Picture 5How to Compile and Execute Java Using Command Prompt Picture 5

Compile the program. Once in the correct directory, you can compile the program by typing javac filename.java into the command line and pressing enter.

If there are any errors with the program or the command line is not correct, the Command Prompt will notify you.

You can find articles on how to fix Java's compilation errors for more details.

How to Compile and Execute Java Using Command Prompt Picture 6How to Compile and Execute Java Using Command Prompt Picture 6

Executing the program. Type the command java filename and then press ↵ Enter. At this point, of course you have to replace "filename" with your actual filename.

After pressing ↵ Enter, your program will be executed. If the machine gives an error or the program crashes while running, and so on, continue with the following troubleshooting method.

Troubleshooting

How to Compile and Execute Java Using Command Prompt Picture 7How to Compile and Execute Java Using Command Prompt Picture 7

Set path. If you want to launch a simple program that has all the files in the same directory, you don't need to do this. However, if you are launching a more complex program with files located in different folders, you need to tell your computer where to find these files.

On Windows: Type java -version command into the Command Prompt window and press ↵ Enter. Based on the Java version shown in the first line, enter the code set path=%path%;C:/Program Files/Java/jdk1.5.0_09/bin into the command line and press ↵ Enter. Replace jdk1.5.0_09 with whatever Java version you have installed.

Make sure you are entering this command in the correct directory where your Java project is located.

On Mac: Type /usr/libexec/java_home -v 1.7 into the Terminal window and press ↵ Enter to make sure Java is installed on the system. Next, type echo export "JAVA_HOME=/$(/usr/libexec/java_home)" >> ~/.bash_profile into the command prompt and then press ↵ Enter. Then restart Terminal.

5 ★ | 1 Vote