How to Compile and Run Java Programs with the Command Line

Table of contents:

How to Compile and Run Java Programs with the Command Line
How to Compile and Run Java Programs with the Command Line

Video: How to Compile and Run Java Programs with the Command Line

Video: How to Compile and Run Java Programs with the Command Line
Video: How To Install Windows 10 2024, December
Anonim

While most program development environments (IDEs) allow you to compile and run programs directly, you can also compile and test programs via the command line. The command line interface on Windows is known as Command Prompt, while on Mac, the same interface is known as Terminal. However, the process for compiling and running a Java program on the command line of both operating systems is essentially the same.

Step

Method 1 of 2: Compiling and Running the Program

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

Step 1. After writing the program code with a text editor like Notepad, save the program with the.java extension

You can name the file as you wish. For example, in this guide, the file name used is "filename".

  • When saving files, don't forget to write the ".java" extension after the file name, and select the All Files option in the Extension field.
  • Know where to save the file containing the program code.
  • If you don't understand Java, read guides on the internet. While studying, try running different types of programs.
Compile & Run Java Program Using Command Prompt Step 2
Compile & Run Java Program Using Command Prompt Step 2

Step 2. Open the command line interface on your system

The way to open this interface differs depending on whether you are using Windows or Mac.

  • Windows: Press Home, then enter cmd in the Run dialog box. After that, press Enter. A Command Prompt window will open.
  • Mac: in the Finder, click the Go tab, then select Applications > Utilities. In the Utilities folder, select Terminal.
Compile & Run Java Program Using Command Prompt Step 3
Compile & Run Java Program Using Command Prompt Step 3

Step 3. Check if Java is installed on the computer with the command

java -version

.

If Java is already installed, you will see the Java version on the screen.

If Java is not already installed, download the Java Development Kit for free from

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 program files

Use the "cd" command, followed by the directory name.

  • For example, if the command line interface is in the folder

    C:\Users\Ayu Rosmalina\Project

    and you save the code in the folder

    C:\Users\Ayu Rosmalina\Project\Fake Address

    insert

    cd Fake Address

  • , and press Enter.
  • You can view the contents of the folder with the command

    dir

  • . After entering the command, press Enter.
Compile & Run Java Program Using Command Prompt Step 5
Compile & Run Java Program Using Command Prompt Step 5

Step 5. After going to the folder where you saved the program, compile it

Enter command

javac filename.java

and press Enter.

  • Errors or errors during compilation will appear in the command line window.
  • Read guides on the internet to find out how to fix compiler errors in Java.
Compile & Run Java Program Using Command Prompt Step 6
Compile & Run Java Program Using Command Prompt Step 6

Step 6. Run the program by entering the command

java filename

.

After that, press Enter. Replace "filename" with the filename of your Java program.

After pressing Enter, your program will run. Read the next steps to resolve errors that may occur while running the program

Method 2 of 2: Troubleshooting

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

Step 1. Set the PATH if you are going to run a complex program that puts files in various directories

If you're only running a simple program, and all of the program files are in the same folder, you don't need to do this step.

  • Windows: Enter command

    java -version

    and press Enter. Take note of the Java version that appears on the screen. After that, enter the command

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

    and replace jdk1.5.0_09 with the java version you noted earlier. Then, press Enter.

    Make sure you point the command line to the application storage folder before running the above command

  • Mac: enter command

    /usr/libexec/java_home -v 1.7

    and press Enter. After that, enter the command

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

  • , press Enter, and restart Terminal.

Recommended: