Instructions on how to compile and execute Java using Command Prompt
- The tricks in Windows 10 you may not know
- How to map network drives with Command Prompt in Windows
- 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.
- 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.
- 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.
- On Windows : Click
Home
and then type cmd . PressEnter
to open the Command Prompt. - 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.
- 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 codeset path=%path%;C:Program FilesJavajdk1.5.0_09bin
into the command line and press Enter . Replacejdk1.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. - 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 codeecho export "JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.bash_profile
at the command prompt and press Enter . Then restart Terminal.
Refer to some more articles:
- How to open Command Prompt on Windows 10, 8, 7, Vista and XP
- How to compile a kernel - the way on CentOS system
- Check and fix hard drive errors with CMD on Windows
Having fun!
You should read it
- How to Compile & Run Java Program Using Command Prompt
- How to Compile and Execute Java Using Command Prompt
- How to Compile a Java Program
- How to Check Your Java Version in the Windows Command Line
- What is Java? Why choose Java?
- Which career Java programming options are waiting for you?
- How to use the command history function in Command Prompt
- eQuiz - Multiple choice quiz about JAVA
- Basic Java exercises, with sample decoding
- How to open Command Prompt on Windows 10, 8, 7, Vista and XP
- Basic Java syntax
- What is JAVA file? How to open, edit and convert JAVA files