Instructions on how to compile and execute Java using Command Prompt

Let's TipsMake.com refer to the tutorial how to compile and execute Java by Command Prompt on Windows and Mac computers!
  1. The tricks in Windows 10 you may not know
  2. How to map network drives with Command Prompt in Windows
  3. Set up an ad-hoc wireless network on Windows 8 to play WiFi

While many programming environments allow you to compile and execute a program within that program, you can compile and execute with the Command Prompt. Windows and Mac computers all have their own version of Command Prompt (technically, it's called Terminal on Mac OS). However, whether it is Windows or Mac, this process is essentially the same. Let's TipsMake.com refer to the tutorial how to compile and execute Java by Command Prompt offline!

Method 1: Compile and execute

1. Save the program

Instructions on how to compile and execute Java using Command Prompt Picture 1Instructions on how to compile and execute Java using Command Prompt Picture 1

After using a text editor, such as NotePad to create Java programs, save with the extension '.java'. Of course, you can optionally name the file. For the purposes of this tutorial, "filename" will be used to represent your file name.

  1. To make sure your file is saved as a file with the ' .java ' extension, type '.java' behind the file name and select All Files from the extended drop-down menu.
  2. Record the location where you save the file on the system.

2. Open Command Prompt / Terminal

Instructions on how to compile and execute Java using Command Prompt Picture 2Instructions on how to compile and execute Java using Command Prompt Picture 2

Command line access operations on Mac and Windows operating systems are slightly different.

  1. On Windows : Click Home and then type cmd . Press Enter to open the Command Prompt.
  2. On the Mac : In the Finder tool, click the Go tab, select Applications , select Utilities, and then select Terminal .

3. Check if the Java running environment is installed

Instructions on how to compile and execute Java using Command Prompt Picture 3Instructions on how to compile and execute Java using Command Prompt Picture 3

Type java -version into the command line. If Java is installed, you will see a message telling you about the version of Java available in the machine.

If not, you need to install 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 .

4. Navigate to the correct directory of the program

Instructions on how to compile and execute Java using Command Prompt Picture 4Instructions on how to compile and execute Java using Command Prompt Picture 4

Use the cd command with the directory name to change the active directory. For example, if you are running at C:UsersBobProject and want to move to C:UsersBobProjectTitanProject , type cd TitanProject and press Enter .

Alternatively, you can view the current directory list by entering the dir command and pressing Enter .

5. Compile the program

Instructions on how to compile and execute Java using Command Prompt Picture 5Instructions on how to compile and execute Java using Command Prompt Picture 5

After accessing the correct directory, you can compile the program by typing javac filename.java in the command line and pressing Enter . If any error occurs with the program or command line is not correct, Command Prompt will notify you.

6. Execute the program

Instructions on how to compile and execute Java using Command Prompt Picture 6Instructions on how to compile and execute Java using Command Prompt Picture 6

Enter the java filename command and press Enter . Of course, you must replace "filename" with your actual file name.
After pressing Enter, your program will be executed. If the machine reports an error or the program encounters an error while running, continue to perform the troubleshooting procedure (troubleshooting) below.

Method 2: Troubleshooting

1. Set up the path

Instructions on how to compile and execute Java using Command Prompt Picture 7Instructions on how to compile and execute Java using Command Prompt Picture 7

If you want to launch a simple program with all files in the same folder, 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 let your computer know where to find these files.

  1. On Windows : Type java -version command into the Command Prompt window and press Enter. Based on the version of Java displayed in the first line, enter the code set path=%path%;C:Program FilesJavajdk1.5.0_09bin into the command line and press Enter . Replace jdk1.5.0_09 with any version of Java you have installed. Make sure you are entering this command in the correct directory for your Java project.
  2. On the Mac : Type /usr/libexec/java_home -v 1.7 in the Terminal window and press Enter to make sure Java is installed on the system. Next, enter the code echo export "JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.bash_profile at the command prompt and press Enter . Then restart Terminal.

Refer to some more articles:

  1. How to open Command Prompt on Windows 10, 8, 7, Vista and XP
  2. How to compile a kernel - the way on CentOS system
  3. Check and fix hard drive errors with CMD on Windows

Having fun!

4 ★ | 1 Vote