How to Compile and Run a Java Program Using the Command Prompt

Table of contents:

How to Compile and Run a Java Program Using the Command Prompt
How to Compile and Run a Java Program Using the Command Prompt
Anonim

While many development environments allow you to create, compile, and run programs without having to resort to using other tools or software, you can compile and run your own programs written in Java directly from the command line if you wish. On Windows systems the "Command Prompt" must be used, while on macOS systems the "Terminal" window must be used. The procedure to follow to compile and run a Java file is identical on both systems.

Steps

Method 1 of 1: Compile and Run a Java Program

Compile & Run Java Program Using Command Prompt Step 1
Compile & Run Java Program Using Command Prompt Step 1

Step 1. Save the program source code

Writing a program in Java is extremely simple, since it is enough to use any text editor, for example the Windows "Notepad". The important thing is to save the file containing the source code with the ".java" extension. Of course, you can name the file with whatever name you prefer. In our example, we will use the "filename" variable to refer to this information.

  • To be sure that the file is saved with the ".java" extension, write it at the end of the name you have chosen for the file and select the entry "All files" from the "Save as type" or "Files of type" drop-down menu.
  • Make a note of the full path where you decided to save the file.
  • If you are unfamiliar with the Java programming language and don't know how to write a program correctly, refer to this guide. To follow the instructions in this article and learn how to compile and run a program written in Java, you can choose to use the source code of any Java program.
Compile & Run Java Program Using Command Prompt Step 2
Compile & Run Java Program Using Command Prompt Step 2

Step 2. Open a "Command Prompt" or "Terminal" window

Specifically, the procedure to follow to access the command console varies slightly between Mac and Windows.

  • Windows systems:

    go to the ⇱ Home menu, then type in your keyword "cmd" (without quotes). Simply press the Enter key to open a "Command Prompt" window.

  • MacOS systems:

    open a "Finder" window, access the menu "Go", select the item Applications, choose the option "Utility", then select the icon "Terminal".

Compile & Run Java Program Using Command Prompt Step 3
Compile & Run Java Program Using Command Prompt Step 3

Step 3. Verify that Java is installed on your system

Type the command

java -version

inside the command console. If Java is installed correctly, you will see a series of messages appear indicating the version of Java currently installed on the system.

If an error message appears, you will need to download the Java Development Kit from its website and install it on your computer. To download the installation file, you can use the URL:

Compile & Run Java Program Using Command Prompt Step 4
Compile & Run Java Program Using Command Prompt Step 4

Step 4. Go to the directory where you saved the Java file that contains the source code of the program you want to run

To do this, use the command CD followed by the full path to the folder you want to access.

  • For example, if you are currently inside the folder

    C: / Users / Luca / Progetti

    and you need to access the directory

    C: / Users / Luca / Progetti / Titan

    you will need to type the command

    cd Titan

  • followed by pressing the Enter key.
  • To see the list of everything in the directory you are currently in, use the command

    dir

  • then press the Enter key.
Compile & Run Java Program Using Command Prompt Step 5
Compile & Run Java Program Using Command Prompt Step 5

Step 5. Compile a Java program

Once you have reached the folder where the program file you want to compile is located, type the command

javac filename.java

and press the "Enter" key.

  • If any error is encountered or if problems arise in compiling the indicated source code, the command console will notify you clearly.
  • Check out this guide to find out how to fix Java program source code errors detected by the compiler.
Compile & Run Java Program Using Command Prompt Step 6
Compile & Run Java Program Using Command Prompt Step 6

Step 6. Run the program

To run a program written in Java, after compiling it correctly you can use the command

java filename

then press the Enter key. Obviously you will have to replace the variable "filename" with the name you have chosen for the Java file that contains the program's source code.

After pressing the Enter key, the indicated program will be executed. If you receive an error message at this point, or if the program has unexpected behavior, please refer to the "Troubleshooting" section

Troubleshooting

Compile & Run Java Program Using Command Prompt Step 7
Compile & Run Java Program Using Command Prompt Step 7

Step 1. Set the system "path" variable

If you're looking to run a simple program whose files are properly stored within a single folder, chances are you won't have to perform this step. Conversely, if you are dealing with complex software that uses resources located in multiple folders, you need to tell the operating system where to look for the resources required by the program.

  • Windows systems:

    type the command

    java -version

    within the "Command Prompt" window, then press the Enter key. Based on the Java version number shown on the first line of the previous command output, type the code

    set path =% path%; C: / Program Files / Java / jdk1.5.0_09 / bin

    within the "Command Prompt" window, then press the Enter key. Remember to replace the folder "jdk1.5.0_09" with the one for the version of Java installed on your computer.

    Make sure you run the indicated program while you are inside the directory that contains the Java project you want to compile

  • MacOS systems:

    to make sure Java is installed on your system, type the command

    / usr / libexec / java_home -v 1.7

    within the "Terminal" window, then press the Enter key. At this point, run the command

    echo export "JAVA_HOME = / $ (/ usr / libexec / java_home)" >> ~ /.bash_profile

  • within the "Terminal" window, then press the Enter key. After making the changes indicated, close and reopen the "Terminal" window.

Recommended: