A Beginner’s Guide to Compiling & Running Java Programs
A Beginner’s Guide to Compiling & Running Java Programs
While many programming environments will allow you to compile and run a program within that environment, you can also compile and run in Command Prompt on Windows or the Terminal on Mac. The process is nearly the same. This wikiHow article teaches you how to compile and run a Java program using the Command Prompt or Terminal.
Things You Should Know
  • At the command prompt, type "cd" followed by the path your Java program is saved to, then press "Enter."
  • Type "javac [filename] and press "Enter" to compile the program.
  • Type "java [filename]" and press "Enter" to run the Java program after it is compiled.

Save the program. You can create a Java program using a simple text editing program like Notepad or an independent development environment (IDE) such as Eclipse. It's recommended you save the filename as the name of the Java class. Be sure to add the ".java" file extension to the file. To ensure that your file is saved as a .java file, be sure to type .java after the file name and select All Files from the dropdown extension menu. Make note of where you saved the file on your system. If you are unsure how to write a program in Java, check out our tutorial on how to program in Java. You can use any Java program to learn how to compile and run a program.

Open Command Prompt or Terminal Windows cmd. Accessing the command line differs slightly for Mac and Windows. Windows: Click the Windows Start menu and type CMD. Click the Command Prompt icon. Mac: In Finder, press the Go tab, select Applications, select Utilities, then select Terminal.

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. If not, you may need to install the Java Development Kit from their website. It is free and can be found at this link: http://www.oracle.com/technetwork/java/javase/downloads/index.html. If you are not running the latest version of Java, you can update Java in the Java control panel.

Navigate to the folder your program is saved to. Use the command cd followed by the directory name to change folders in the Command Prompt. For example, if your program is saved to "C:\Users\Bob\Project\TitanProject," type cd C:\Users\Bob\Project\TitanProject and press Enter to navigate to that folder. You can see a list of what is in your current directory by typing dir and pressing Enter.

Compile the program. Once you are in the correct directory, you can compile the program. To do so, type javac [filename].java into the command line and press Enter. If you have any errors in your program or difficulty compiling, the command prompt will notify you. You'll need to fix the errors before the program can be compiled.

Run the program. Type java [filename] and press Enter. Of course, replace "filename" with whatever the name of your file is. After pressing Enter, your program should run. If you receive an error or your program fails to run, you must do some troubleshooting.

What's your reaction?

Comments

https://tupko.com/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!