How to Compile and Execute Java Using Command Prompt
Compile and execute
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.
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.
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.
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.
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.
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
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.
You should read it
- How to use the command history function in Command Prompt
- Add Command Prompt to Power User Menu on Windows 10
- How to open Command Prompt on Windows 10, 8, 7, Vista and XP
- How to Make Command Prompt Appear at School
- 6 Best Command Prompt Alternatives for Windows
- How to use Xcopy command in Command Prompt
- Instructions for using Command Prompt
- All Frequency Restart, Shutdown, Remote Off ... with Command Prompt
- Windows prompt
- How to Customize the Font in Windows Command Prompt
- How to Unblock Command Prompt Anywhere
- How to Create a Custom Windows Command Prompt