How to run Java Program in Eclipse

In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of the important IDE (Integrated Development Environment) used for writing Java programs.

Before moving ahead in this section, ensure that Java and the Eclipse IDE is already installed.

Let’s follow the steps given below.

  1. Open the eclipse.exe file by double-clicking it.
  • After eclipse.exe starts running, it asks for the workspace. In the selected workspace, the source code and .class files reside. After that click on the Launch button.

After selecting the workspace at any desired location, press the Launch button. The eclipse then shows the following.

  • Create a Java project. Click on the File -> New -> Java project

Another way is to press Alt + Shift + N then select Java project. After selecting Java Project, we get the following window.

In the project name, enter the project name of one’s choice and press the Next button. We have entered MyFirstProject as the project name. The Next button takes to the following window.

As we are not concerned about the modules, the check box corresponding to the module-info.java file should be unchecked.

 Now, click on the Finish button, following window appears on the screen.

Now go to the File tab -> New-> Class.

The other way is directly click on the class logo, present just below the menu bar.

After clicking the class logo, the following screen appears.

In the name field, enter the Name of the class that is desired in the code. We have entered HelloExample. Press the Finish button. It creates a HelloExample.java file. The file has the class HelloExample.

In this file, we can write the Java code. For now, let’s print a welcome statement.

FileName: HelloExample.java

public class HelloExample

{

// main method

public static void main(String argvs[])

{

// A given character array

char[] chArr = {‘w’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘ ‘ , ‘t’, ‘o’, ‘ ‘, ‘T’, ‘u’, ‘t’, ‘o’, ‘r’, ‘i’, ‘a’, ‘l’, ‘A’, ‘n’, ‘d’, ‘E’, ‘x’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’};

// Converting the character array to a string

String str = new String(chArr);

// printing the string

System.out.println(str);

}

}

Now, press the run button.

Pressing the run button gives the following output on the console.

Note that output is only generated, if the code is error-free.

Pin It on Pinterest

Share This