How to Compile and Execute Java Using Command Prompt

Compile and execute

How 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 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 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 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 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 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 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

May be interested

  • Installation instructions CInstallation instructions C
    there are several free and available ides for compiling and executing c programs. you can choose dev-c ++, code :: blocks, or turbo c. however, the most popular and commonly used options are especially dev-c ++ and c programs in this series are also compiled and executed in dev-c ++.
  • Add Command Prompt to Power User Menu on Windows 10Add Command Prompt to Power User Menu on Windows 10
    on windows 10 build 14971, microsoft replaced command prompt and command prompt (admin) with windows powershell. according to microsoft, this change will bring the best command line experience to users. but in fact, users prefer to use command prompt rather than using powershell.
  • How to Make Command Prompt Appear at SchoolHow to Make Command Prompt Appear at School
    this wikihow teaches you how to access command prompt on a windows 10 school computer. although you may be able to open command prompt if only the file path to command prompt is blocked, there is no way to bypass an administrator lock on...
  • 6 Best Command Prompt Alternatives for Windows6 Best Command Prompt Alternatives for Windows
    do you find the command prompt a bit complicated and feel you need a tool that is easier to use? this is where other terminal emulators come into play!
  • How to use Xcopy command in Command PromptHow to use Xcopy command in Command Prompt
    knowing how to use xcopy command in command prompt, you will easily use commands in cmd professionally. in particular, the xcopy command is extremely useful for those who copy and copy data on the computer.
  • 6 Cool Tricks You Can Do in Windows Command Prompt6 Cool Tricks You Can Do in Windows Command Prompt
    command prompt has a ton of capabilities, and many people doubt they know what a few lines of code can do. next time you're bored with your pc, try these cool command prompt tricks for yourself.
  • Instructions for using Command PromptInstructions for using Command Prompt
    deep in windows is a command-line world that is obscure. in this article, we will show you how to solve errors and make your computer more secure.
  • All Frequency Restart, Shutdown, Remote Off ... with Command PromptAll Frequency Restart, Shutdown, Remote Off ... with Command Prompt
    often users often turn off the computer, restart the computer ... using the shutdown options on the start menu. however, few of us know that using command prompt can also shutdown, restart the computer, schedule a computer shutdown or enable hibernation, ... and even if you have admin rights you can turn off other computers remotely.
  • Run remote commands on Cisco routers from the computerRun remote commands on Cisco routers from the computer
    can you place the cursor at the windows command prompt and run a command on the router? usually, you will first have telnet or ssh to connect to the router to be able to execute commands? this article will show you how to simultaneously run router commands without having to exit the windows command prompt, which is very convenient for control and
  • Open folder in Command Prompt (CMD)Open folder in Command Prompt (CMD)
    do you need to access a certain folder from the command prompt or windows command prompt? although it looks classic, this program is really easy to use if you know some basic commands. today's tipsmake will show you how to use the 'cd' command to open a folder in windows command prompt. besides, you will also learn great tips to immediately open the command prompt in any folder on windows file explorer.