Run your First Java Program

689
Run your first java program
Advertisement

As We had already discussed about Java Hello World Program in the previous posts. Till now if you haven’t run your Java program then You first need to Setup JDK and Integrated Development Environment(IDE) or any Code Editor. If you had any confusion about setting up JDK and IDE you should check How to install JDK in your Computer? as well as you go through YouTube and watch it.

As of now we assume that you had already setup you JDK as well as you had setting up you IDE or Code Editor whatever you like to run your Java Program.

Run your First Java Program using IDE

As I discussed about IntelliJ IDEA we are using IntelliJ as our default IDE.

Create the Project in the IntelliJ IDEA

Step 1: Simply Open you IDE and select Create New Project.

Create a new project using IntelliJ IDE
Create a new project using IntelliJ IDE

Step 2: Then in the next Step Select Java from the left pane. If the Project SDK Section has No SDK detected simply click on the drop down menu and select your JDK version and leave everything as default and Click enter as shown in the below Image.

Select Java as the Project and set the Project SDK
Select Java as the Project and set the Project SDK

In the next Section leave it as default and click next.

Step 3: Then In the next Window IntelliJ IDEA asks you for the Project Name. As I had entered DailyJavaConcept as my Project name same as Enter your project name and click finish.

Give a name to your Java Project
Give a name to your Java Project

It will take a minute or two to prepare the workspace for our Java Development.

Step 4: In the Left Pane you will get you Project Name. Expand the Project Folder and then analyze the project Structure.

IntelliJ IDEA IDE Project Structure
IntelliJ IDEA IDE Project Structure

Step 5: Under the Project you will found a folder named src. In this folder we will create our all java files and run them.

So for creating you First Java Program, right click on the src Folder then select new and select Java Class.

right click on src folder -->New -->Java Class

Then you will get the window like below and then Enter you Java Program name as i had entered Hello_Java.

Create the Java Class file to run the Java Program
Create the Java Class file to run the Java Program

after entering the Java file name Hit enter and you will see a new file with the name of your java program will be created under the src folder.

After that the IDE Interface will look like as the image below:

Create the Java Class file to run the Java Program
Write the first Java Program as a beginner

If you didn’t learn the basics of the Java Hello World program till now then learn by visiting the link or copy the below Java First Program and paste into the IDE.

//First Java Program

class Hello_Java {

    public static void main(String[] args) {

        System.out.println("Hello World!");

    }
}

After pasting the program you should check that the Java filename and the Class name of the Java program should be same otherwise you will get the error.

Create the Configuration to Run your Java Program

Step 6: Then you Click on the App Configuration Button on the top bar nd then Click on the + and then Select the Application from the drop down menu.

Create the Configuration to run the Java Program
Create the Configuration to run the Java Program

Step 7: After Selecting the Application,

  1. Change the name of the Configuration for running the Program as I renamed it to App.
  2. Follow the Main Class label and click the 3 dots in the last.
  3. Then select the Java Program to run, as We have Hello_Java then select and click ok and again ok to save the congiguration.
Setting up the Java file in the Configuration
Setting up the Java file in the Configuration

Now everything is done and you just need to run the program.

Step 8: For running the Java program click on the green color triangular button next to the Configuration then your program start compiling and start running and within a moment you will get the output under the Run tab in the below section of the IDE.

Run your Java Program
Run your Java Program

Note: This process of running the Java program is same for both Windows and Linux.

Run your First Java Program using Code Editor

In this Section I will only cover to run the Java Program using the Notepad++(Windows) or Notepadqq(Linux) because both the process is same as you can try to runt you java program using the Visual Code Editor by your own if you get ang issue then comment below.

Step 1: Open your Notepad and copy and paste the program below program:

//First Java Program

class Hello_Java {

    public static void main(String[] args) {

        System.out.println("Hello World!");

    }
}

Step 2: Then save the program with the name of the class name with the extension .java for example Hello_Java.java is the file name for the program given above.

Now open you Terminal / Command Prompt and type the below command to first compile the Java program and then run the java program.

javac Hello_Java.java : The javac command use to compile the Java file into the Bytecode.

java Hello_Java : The java command use to run the Java program. java command run the generated .class file.

Run Java Program using the Terminal
Run Java Program using the Terminal

As I had already discussed all the ways to run the Java program if you need further assistance then you can also follow the video below:

These are the steps to run the Java program. In the most of the Tutorial we will use the IntelliJ IDE for ease. Otherwise if you want to run the Java program using the Terminal / Command Prompt you can choose you own way.

I hope you like this post. If you have any doubt then feel free to comment in the below section.

You could find the below post useful for your knowledge: