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
Method 1 of 2: Compile and run
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.
Step 2. Open the Command Window/Terminal
Opening the command prompt is slightly different on the Mac than it does on Windows.
Press ⇱ Home and type cmd. Press ↵ Enter to open the command prompt.
In Finder, tap the tab go, and select your Programs > Utilities and after that Terminal.
Step 3. Check if Java is installed
java -versionon 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:
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\Projectand want to go to
C:\Users\Bob\Project\TitanProject, then you feed
cd TitanProjectand press ↵ Enter.
- You can get a list of what is in the current directory using the code
dirthen press ↵ Enter.
Step 5. Compile the program
Once you are in the correct directory, you can compile the program with the code
javac filename.javaand 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.
Step 6. Run the program
java filenameand 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
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.
java -versionat 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\binat 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
/usr/libexec/java_home -v 1.7in 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_profilein the terminal and press ↵ Enter. Now start the Terminal again.