Instructions on how to compile and execute Java using Command Prompt

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

May be interested

  • How to activate Ctrl + C / Ctrl + V to execute paste command in Command Prompt window?How to activate Ctrl + C / Ctrl + V to execute paste command in Command Prompt window?
    windows 10 finally supported with ctrl + c and ctrl + v keys to paste in the command prompt window. however, by default this feature is not enabled and you have to take a few simple steps to activate it.
  • 10 useful commands in Windows you should know10 useful commands in Windows you should know
    command prompt is used to execute batch files, perform tasks quickly, help you troubleshoot and solve some windows problems when the system crashes. however, not all commands in windows are useful and must be done regularly. with the 10 command lines in the following article it is very useful even if you are not an administrator.
  • The most complete and easy way to use CMD (Command Prompt)The most complete and easy way to use CMD (Command Prompt)
    instructions on how to use the cmd command line tool cmd (command prompt) from a to z. the most basic knowledge about the cmd tool that you need to know
  • Add Command Prompt to Power User Menu on Windows 10Add Command Prompt to Power User Menu on Windows 10
    on windows 10 build 14971, microsoft replaced command prompt and command prompt (admin) with windows powershell. according to microsoft, this change will bring the best command line experience to users. but in fact, users prefer to use command prompt rather than using powershell.
  • How to Make Command Prompt Appear at SchoolHow to Make Command Prompt Appear at School
    this wikihow teaches you how to access command prompt on a windows 10 school computer. although you may be able to open command prompt if only the file path to command prompt is blocked, there is no way to bypass an administrator lock on...
  • 6 Best Command Prompt Alternatives for Windows6 Best Command Prompt Alternatives for Windows
    do you find the command prompt a bit complicated and feel you need a tool that is easier to use? this is where other terminal emulators come into play!
  • How to use Xcopy command in Command PromptHow to use Xcopy command in Command Prompt
    knowing how to use xcopy command in command prompt, you will easily use commands in cmd professionally. in particular, the xcopy command is extremely useful for those who copy and copy data on the computer.
  • 6 Cool Tricks You Can Do in Windows Command Prompt6 Cool Tricks You Can Do in Windows Command Prompt
    command prompt has a ton of capabilities, and many people doubt they know what a few lines of code can do. next time you're bored with your pc, try these cool command prompt tricks for yourself.
  • All Frequency Restart, Shutdown, Remote Off ... with Command PromptAll Frequency Restart, Shutdown, Remote Off ... with Command Prompt
    often users often turn off the computer, restart the computer ... using the shutdown options on the start menu. however, few of us know that using command prompt can also shutdown, restart the computer, schedule a computer shutdown or enable hibernation, ... and even if you have admin rights you can turn off other computers remotely.
  • Open folder in Command Prompt (CMD)Open folder in Command Prompt (CMD)
    do you need to access a certain folder from the command prompt or windows command prompt? although it looks classic, this program is really easy to use if you know some basic commands. today's tipsmake will show you how to use the 'cd' command to open a folder in windows command prompt. besides, you will also learn great tips to immediately open the command prompt in any folder on windows file explorer.