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
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
Command line access operations on Mac and Windows operating systems are slightly different.
- On Windows : Click
Homeand then type cmd . PressEnterto 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
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
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
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
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
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 -versioncommand 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_09bininto the command line and press Enter . Replacejdk1.5.0_09with 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.7in 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_profileat 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!