How to compile and run Java program


JavaViews 2398

How to compile and run Java program – Let us see how to write, compile, and run a simple java program. We can either use a command prompt or any IDE like Eclipse to compile and run any Java program. In this tutorial, we will see both methods.

Prerequisite

We should have the below prerequisites before we start writing our first Java program then we will learn how to run a java program?

  • Install Java in your system.
  • A text editor or IDE like Eclipse.

Simple Java Program

Below is a simple Java program to print a line of the statement.

public class MyJavaProgram {

  public static void main(String[] args) {
    System.out.println("This is my first Java program");

  }

}
This is my first Java program

Explanation of the Java Program

Let us see in detail what each line means in the simple Java program given above.

public class MyJavaProgram {

Here, public is the access modifier which means that this class can be accessed anywhere. Next is the keyword class followed by the class name. We should always remember that the class name should be the same as the filename.

public static void main(String [] args) {

public  – states that this method can be accessed from outside the class

static – which means we don’t need to create an object to run this method

void – does not return any value

main – it is the method name. JVM uses this as an entry point to start the execution

string[] args – This is nothing but an array of string arguments for the main method which we can pass through the command prompt during execution.

System.out.println(“This is my first Java Program”); – This prints the output. The value/string which we need to print must be passed within this method.

Every java statement terminates with a semicolon(;).

Now let’s see how we can save this as a Java file, compile and run a java program.

Steps to save Java file using Text Editor

  • Open any text editor like notepad
  • Copy and paste the above code in the text editor
  • Save the file as “MyJavaProgram.java” under a specific location. You should always remember that the class name and file name should be the same.

Compile and Run Java Program using Command Prompt

How to run a java program – Before we compile and run a java program, we need to set the java path in order to use javac and java commands. We don’t require this step if we are saving the java file in the JDK/bin directory. We can either set the java path temporarily or permanently

Set temporary java path

  1. Copy the JDK/bin directory path
  2. Open the command prompt
  3. Write the below command: set path=”JDK/bin path”

set path= “C:\Program Files\Java\jdk-13\bin”

If we do not set java path before compiling, we will get an error “javac is not recognized as an internal or external command, operable program or batch file”

Set permanent java path

To permanently set the java path, we need to provide the JDK/bin path under System environment variables. You can follow the below steps:

  1. Right-click on “My computer” -> Click on properties
  2. Click on Advanced System settings
  3. Next, click on Environment variables
  4. Click on New under User variables
  5. Write path under the variable name
  6. If already the path variable is present, you can ignore steps 4 and 5. Instead, click on the Edit button after selecting the path variable.
  7. Paste the JDK/bin path
  8. Click on OK in all the dialogs

Now we are all set to compile and run the java program.

Run java program (How to run a java program)

Before we run a java program, we need to compile to see if there are any errors.

  1. Open the command prompt
  2. Change the directory to the path where we have saved the java file using the cd command.
  3. Now, compile the java file using the command javac.
  4. javac MyJavaProgram.java where MyJavaProgram is the java file name.
  5. If there are no errors, it means that the file is compiled successfully.
  6. Next, we execute the java program using the command java.
  7. java MyJavaProgram.java where MyJavaProgram is the java file name.
  8. If successful, it prints the output in the command prompt itself

After successful compilation, we can see that the Java compiler creates a .class file in the same path where we have saved the .java file.

Compile and Run Java Program using Eclipse IDE

How to run a java program – We can also compile and run java programs using IDE. In this tutorial, we will learn how to save java file and execute using the Eclipse IDE.

Step 1: Install Eclipse IDE from the eclipse website

Step 2: Create a new Java Project

File -> New -> Java Project

Step 3: Enter the project name (Eg: FirstJavaProject) and click on Finish button

Step 4: Create a new Java class

Right click on the Java Project-> New -> Class

Step 5: Enter the class name and click on Finish button

Eg: MyJavaProgram

Step 6: Copy and paste the above code

Step 7: Save the file. It automatically saves as a Java file since we have created a Java project

Step 8: Click on the Run button in the toolbar menu

Step 9: The java program will compile and run and display the output in the console window below

Compile and Run a java program

Conclusion

In this tutorial, we have learned how to run a java program (how to compile and run a java program) using both command prompt and Eclipse IDE.

Reference

Translate »