How to compile and run a Java program using the command line

Table of contents:

How to compile and run a Java program using the command line
How to compile and run a Java program using the command line
Anonim

While many programming environments allow programs to be compiled and run, they can also be compiled and run using the command line. Windows and Mac have their own versions of the command line, on Mac OS it's called Terminal. The compilation and run process for Windows and Mac is almost identical.

Steps

Method 1 of 2: Compiling and Running

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

Step 1. Save the program

After creating a Java program using a text editor such as NotePad, save it with the.java extension. The file name, of course, can be anything. In this tutorial, we will use "filename" as the name of the file.

  • To save the file as.java, after the file name write.java and select All files in the drop-down menu for selecting extensions.
  • Remember where you saved the file.
  • If you don't know how to write a Java program, look for additional tutorials on that. However, you can use any Java program to learn how to compile and run programs.
Compile & Run Java Program Using Command Prompt Step 2
Compile & Run Java Program Using Command Prompt Step 2

Step 2. Open Command Prompt / Terminal

Command line access is slightly different for Mac and Windows.

  • Windows.

    click start then type cmd.

    To open Command Prompt, press ↵ Enter.

  • Mac.

    in Finder click on the tab Transition, select Programs, then - Utilities and click on 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

At the command prompt, enter

java -version

… If java is installed, you will see a message with the version of Java installed.

If not, you need to install Java Development Kit from their website. It can be downloaded for free from the link:

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

Step 4. Navigate to the desired folder

To change the working directory use the command cd, and then enter the name of the directory.

  • For example, if you are currently in the directory

    C: \ Users \ Bob \ Project

    and want to change it to

    C: \ Users \ Bob \ Project \ TitanProject

    , enter

    cd TitanProject

    and press ↵ Enter.
  • If you enter

    dir

    and press ↵ Enter, you can see a list of files that are in this directory.
Compile & Run Java Program Using Command Prompt Step 5
Compile & Run Java Program Using Command Prompt Step 5

Step 5. Compile the program

Once you find yourself in the right directory, you can compile the program - enter in the command line

javac filename.java

and hit enter.

  • If your program has any errors or has difficulty compiling, the command line will warn you about it.
  • For more help, check out our article on 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

Enter

java filename

and press ↵ Enter. Replace "filename" with your filename of course.

After pressing ↵ Enter, your program should start. If you receive an error message or your program does not work, use the troubleshooting method

Method 2 of 2: Troubleshoot errors

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're using a simple program that has files in the same directory, you probably don't need to. However, if you are using a more complex program with files in multiple directories, you will need to tell your computer where to look for those files.

  • Windows.

    at the command prompt, enter

    java -version

    and press ↵ Enter. Based on the Java version listed on the first line, type at the command prompt

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

    and press ↵ Enter. Replace jdk1.5.0_09 the version of Java you have installed.

    Enter this command when you are in the directory with your Java program

  • Mac.

    to make sure you have Java installed, enter the command in the terminal

    / usr / libexec / java_home -v 1.7

    and press ↵ Enter. Then enter

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

    and press ↵ Enter. Then restart the terminal.

Popular by topic