How to compile and run a Java program using the command line

Author: Carl Weaver
Date Of Creation: 27 February 2021
Update Date: 2 July 2024
Anonim
How to Compile and Run Java Program from Command Prompt
Video: How to Compile and Run Java Program from Command Prompt

Content

While many programming environments allow programs to be compiled and run, they can also be compiled and run using the command line. Windows and Mac have their own versions of the command line, on Mac OS it's called Terminal. The compilation and run process for Windows and Mac is almost identical.

Steps

Method 1 of 2: Compiling and Running

  1. 1 Save the program. After creating a Java program using a text editor such as NotePad, save it with the .java extension. The file name, of course, can be anything. In this tutorial, we will use "filename" as the name of the file.
    • To save the file as .java, after the file name write .java and select All files in the drop-down menu for selecting extensions.
    • Remember where you saved the file.
    • If you don't know how to write a Java program, look for additional tutorials on that. However, you can use any Java program to learn how to compile and run programs.
  2. 2 Open Command Prompt / Terminal. Command line access is slightly different for Mac and Windows.
    • Windows: click Start, then enter cmd... To open Command Prompt, click ↵ Enter.
    • Mac: in Finder click on the tab Transition, select Programs, then - Utilities and click on Terminal.
  3. 3 Check if Java is installed. Enter java -version at the command line. If java is installed, you will see a message with the version of Java installed.
    • If not, you need to install Java Development Kit from their website. It can be downloaded for free from the link: http://www.oracle.com/technetwork/java/javase/downloads/index.html.
  4. 4 Navigate to the desired folder. To change the working directory use the command cd, and then enter the name of the directory.
    • For example, if you are currently in the C: Users Bob Project directory and want to change it to C: Users Bob Project TitanProject, enter cd TitanProject and click ↵ Enter.
    • If you enter dir and press ↵ Enter, you will be able to see a list of files that are in this directory.
  5. 5 Compile the program. Once you are in the correct directory, you can compile the program - type javac filename.java into the command line and press enter.
    • If your program has any errors or has difficulty compiling, the command line will warn you about it.
    • For more help, check out our article on How to Fix Compiler Errors in Java.
  6. 6 Run the program. Enter java filename and click ↵ Enter... Replace "filename" with your filename of course.
    • After clicking ↵ Enter your program should start. If you receive an error message or your program does not work, use the troubleshooting method.

Method 2 of 2: Troubleshoot errors

  1. 1 Set the path. If you're using a simple program that has files in the same directory, you probably don't need to. However, if you are using a more complex program with files in multiple directories, you will need to tell your computer where to look for those files.
    • Windows: at the command prompt enter java -version and press ↵ Enter... Based on the Java version listed on the first line, type set path =% path%; C: Program Files Java jdk1.5.0_09 bin at the command line and click ↵ Enter... Replace jdk1.5.0_09 the version of Java you have installed.
      • Enter this command when you are in the directory with your Java program.
    • Mac: to make sure you have Java installed, enter the command / usr / libexec / java_home -v 1.7 in a terminal and press ↵ Enter... Then enter echo export "JAVA_HOME = $ (/ usr / libexec / java_home)" ~ / .bash_profile and click ↵ Enter... Then restart the terminal.

Tips

  • Again, it is important that you download and install the Java JDK on your computer. Download link: http://www.oracle.com/technetwork/java/javase/downloads/index.html.