Writing Your First Java Program

Hello World!

Traditionally, “Hello World” is the first program you write in a new programming language as you jump into the new world of development. Let’s create the following sample program so that we know the installation is correct.

public class HelloPrinter {

   public static void main(String[] args) {

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

   }
} 

All this program does it output “Hello, World!” to the screen, or the console.

Eclipse Workspace

After clicking the program icon to launch Eclipse, the first question you will be asked is regarding your workspace location.  What is your workspace?  Your Eclipse workspace is a location on your computer where all the files and folders for your Eclipse projects are organized. Typically, you create and use one workspace so that all your files are in the same location.  Eclipse asks you to locate your workspace when launched.  Since this is the first time Eclipse has ran, it is now creating your workspace.  Note the default location in the location box.  Depending on your system, it is typically:

Windows: C:/Users/YOUR-LOCAL-ACCOUNT-NAME/eclipse-workspace

Mac: /Users/YOUR-LOCAL-ACCOUNT-NAME/Documents/workspace

You can also create your own workspace folder if desired.  After selecting the folder, click Launch to finish launching Eclipse.

The first time you launch Eclipse, you see the welcome screen.  Down on the bottom right side, you will see a checkmark to “Always show Welcome at start up”.  Uncheck it and close the Welcome tab. 

Create a Java Project in Eclipse

Now, you’re ready to create your first project with your first Java file.  There are three ways to create a new Java project.

  • Click on the File menu and select New →Java Project.
  • Right click in the Project Explorer and select New → Java Project.
  • Click on the New button ( ) on the toolbar and select Java Project.

The very first time you create a Java project, you will need to find it. Click on the File menu and select New →Other → Java Project and click Next.

Think of a Java project as a folder on your Desktop computer.  This folder is where all the files needed for the specific project will reside.  Compare this to how you use folders to organize your Desktop.  You place all of your school work inside of a folder, possibly divided out by class (English 101) so that you can quickly locate files needed for a specific class (like your English essay).

To create your Java project, provide a project name and information about the JRE.  For your first project, type in the name GettingStarted.  Typically, the execution environment JRE matches what Eclipse installed.  The only concern here would be if the environment would be blank.  If that happens, a quick Google search demonstrates how to set the execution environment in the Preferences for Eclipse. It should default to JavaSE-16.

Down at the bottom, uncheck the “Create module-info.java file” option.

Next, create Finish.  

Since this is your first Java project, Eclipse will ask you to open the Java perspective. Agree to open the perspective.

Possible Error: Creating a module-info.java file

Important note: if you click next instead of finish, Eclipse will prompt you to create a module-info.java file.  Our project does not need a module-info.java file.  This file is a module descriptor file that allows you to specify the additional libraries or tools your program needs to run.  Our projects do not need additional tools or libraries to run – it will just use the components in the JRE.  If your program has a module-info.java file, the easiest way to fix this is to recreate the project. Delete it from the Package Explorer and create it again with the module-info.java file.

The folder to hold the necessary files for the GettingStarted program has been created.  At this point, there is not Java program yet.  It’s time to create a java program to run.

Create a Java Class in Eclipse

The program will be contained within a Java class.  These are special java files with a .java extension.  They have a file name that will match the class declaration.

There are three methods to creating a new Java class in Eclipse:

  • From the File menu and select New → Class.
  • Right click in the Package Explorer and select New → Class.
  • From the toolbar, click on the class drop down button ( ) and select class ( ).

All these options will launch the new Java Class Wizard.  Let’s note a couple of important items in this wizard.

Source Folder

The source folder contains the location where the new java class file will reside.  All Java files will go inside the src folder.  The compiler will look for the java classes inside that folder first.

Package

As we start coding, the Java classes will be in the root of the src folder, commonly called the default package.  If there is content inside the text box, delete it until it says default package.  Later, we will divide out our classes by their functionality and use packages to organize those classes.  At this time, it’s not necessary as we get started with simple programs that only contain one class.

Name

The class name field gives us a place to name our Java classes. The Java class naming convention (direct from Oracle) is as follows:

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html

Name the class HelloPrinter.  

Superclass and Interfaces

We will discuss superclasses and interfaces later but understand that all classes inherit components from a superclass.  In Java, this superclass is called Object.  You can think of it as the parent of all Java classes.  All other classes under the Object class get some components from the superclass that we will discuss later.

Method stubs: Inserting the main( ) Method automatically

Java program always need a main method in one of its classes.  The main method is an instruction for the Java compiler to tell it where to start the program.  You have a couple of options on how to add in a main method into a Java class but the easiest method is while creating the Java class initially.  You can click the checkmark next to public static void main(String[] args) to have the wizard place the method into this class.  If you forget, you can add one after the class is created too.

Finishing the Wizard

Finally, click Finish.  If this is the first Java class Eclipse has created, it asks if you want to open this perspective.  Click Yes.  The newly created class will open in a Java editor so that you can modify the new course.  It will also appear in the Package Explorer, inside the src folder, inside the GettingStarted project.

Your screen may look like this:  

Inserting the Main Method after creating the class

If you do not have a main method, there are two other ways you can use to insert the method inside the public class.  First, place your cursor after the public class HelloPrinter { line but before the closing }

• Type in: public static void main(String[] args) {  Press enter twice and type in the closing }

• Type in the word main and click Ctrl + Space to find template proposals.  Select main – main method from the list.

Notice that the main method goes between the opening { and closing } for the HelloPrinter class.  In other words, the main method is contained inside the HelloPrinter class.  

Adding a Java statement

Next, we will place code inside the main method to run when the program is ran.  This code will go between the opening and closing curly brackets that the main method inserted.  

Inside the main method, type in the following statement:

System.out.println(“Hello, World!”);

Notice the placement of the line, the capitalization and the punctuation.

System.out.println, which stands for “print line”, displays what it is told to print on the screen based on what is inside the parenthesis. Like most statements, the printline statement ends with a semicolon (;).

Running Your First Java Program

How do we get from the .java file to printing the message on the console screen?  Eclipse needs to save and run the program. 

To start the Java program, perform one of the following tasks:

  • Click on Run button from the toolbar
  • Press Ctrl+F11
  • Click on the Run menu and select Run 

The console will appear with the text Hello, World! displaying.

To us, it appears as a simple task of clicking a green run button but a lot happens in the background to get the output from the source code to the screen.  

Up Next: Components of Java Programs