Compiling and Running a Java Program via Command Prompt: 7 Steps (with Pictures)

Table of contents:

Compiling and Running a Java Program via Command Prompt: 7 Steps (with Pictures)
Compiling and Running a Java Program via Command Prompt: 7 Steps (with Pictures)
Anonim

While many programming environments provide the ability to compile and run a program, you can also do this using the Command Prompt. Both Windows and Mac have their own versions of a command window, although under Mac OS it is called the Terminal. The process is essentially the same for both Windows and the Mac

Steps

Method 1 of 2: Compile and run

Compile & Run Java Program Using Command Prompt Step 1

Step 1. Save the program

After you create your Java program using a text editor, such as Notepad, save it with the.java extension. Of course, the file name can be anything. For the purpose of this guide, "filename" will be used instead of your actual filename.

  • To make sure your file is saved as.java, paste.java after the filename, and select All files via the extensions drop-down menu.
  • Pay close attention to where you saved the file on your system.
  • If you don't know how to program in Java, first read a little more about programming in Java. To learn how to compile and run a program, you can use any Java program.
Compile & Run Java Program Using Command Prompt Step 2

Step 2. Open the Command Window/Terminal

Opening the command prompt is slightly different on the Mac than it does on Windows.

  • Windows:

    Press ⇱ Home and type cmd. Press ↵ Enter to open the command prompt.

  • Mac:

    In Finder, tap the tab go, and select your Programs > Utilities and after that Terminal.

Compile & Run Java Program Using Command Prompt Step 3

Step 3. Check if Java is installed

type

java -version

on the command line. If java is installed you will see a message which version of Java is involved.

If not, you may need to download the Java Development Kit from the official website and install the program. It's free and can be found at this link:

Compile & Run Java Program Using Command Prompt Step 4

Step 4. Navigate to the correct folder

Use the command CD followed by the directory name, to change your working directory.

  • Set up your work

    C:\Users\Bob\Project

    and want to go to

    C:\Users\Bob\Project\TitanProject

    , then you feed

    cd TitanProject

    and press ↵ Enter.
  • You can get a list of what is in the current directory using the code

    dir

    then press ↵ Enter.
Compile & Run Java Program Using Command Prompt Step 5

Step 5. Compile the program

Once you are in the correct directory, you can compile the program with the code

javac filename.java

and press Enter.

  • If you see any error messages, or if there are problems with compiling, you will see this in the command window.
  • Read the wikiHow article on fixing compiler errors in Java for more info.
Compile & Run Java Program Using Command Prompt Step 6

Step 6. Run the program

type

java filename

and press ↵ Enter. Replace 'filename' with the actual name of your file, of course.

Press ↵ Enter and your program should start. If you get an error message, or if you can't get your program running, see the Troubleshooting section

Method 2 of 2: Troubleshooting

Compile & Run Java Program Using Command Prompt Step 7

Step 1. Set the file path

If you're working with a simple program with all files in the same folder, you probably won't need to do this. However, if you're running a more complex program with files in multiple directories, you'll need to specify where the computer can find these files.

  • Windows:

    type

    java -version

    at the prompt and press ↵ Enter. Depending on the version of Java as indicated on the first line, type

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

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

    Before using this command, make sure you are in the directory of your Java project

  • Mac:

    type

    /usr/libexec/java_home -v 1.7

    in the terminal and press ↵ Enter to make sure Java is installed on your system. Then type

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

    in the terminal and press ↵ Enter. Now start the Terminal again.

Popular by topic