While many programming environments will allow you to compile and run a program within the environment, you can also compile and run using Command Prompt. Both Windows and Mac have their own versions of Command Prompt, although it is...
Method 1 of 2:
Compiling and Running
Save the program. After using a text editor, such as NotePad, to create your Java program, save the program with a .java extension. Of course, the file name can be anything. For the purposes of this tutorial, "filename" will be used as a placeholder for your file name.
Open Command Prompt/Terminal. Accessing the command line differs slightly for Mac and Windows.
Check that Java is installed. Type java -version into your command line. If java is installed, you will see a message stating what version of Java is currently installed.
Navigate to the correct folder. Use the command cd followed by the directory name to change your working directory.C:UsersBobProject and wanted to get to C:UsersBobProjectTitanProject , enter in cd TitanProject and press ↵ Enter.dir and pressing ↵ Enter.
Compile the program. Once you are in the correct directory, you can compile the program by typing javac filename.java into the command line and pressing enter.
Run the program. Enter in java filename and press ↵ Enter. Of course, replace "filename" with whatever the name of your file actually is.Method 2 of 2:
Troubleshooting
Set your path. If you are running a simple program with all of the files in the same directory, you will probably not need to do this. However, if you are running a more complex program with files in multiple directories, you will need to tell the computer where to look for these files.java -version into the command prompt and press ↵ Enter. Based off the version of Java displayed on the first line, type in set path=%path%;C:Program FilesJavajdk1.5.0_09bin to the command prompt and press ↵ Enter. Substitute jdk1.5.0_09 with whatever version of Java you have installed./usr/libexec/java_home -v 1.7 into the terminal and press ↵ Enter to ensure Java is installed on your system. Then, type echo export "JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.bash_profile into the terminal and press ↵ Enter. Restart the terminal afterwards.Update 05 March 2020
ncG1vNJzZmismaXArq3KnmWcp51ktbDDjK2mZpufor2quMRmZJqmlGJ6s8HNZqGarpFivbO7xquYpmWlqLavs4ycpqalkaOxbrzRqKSprA%3D%3D