Java - SE - Application HelloWorld

This is an easy JAVA SE tutorial for a classic Hello World! code.

Let's start this JAVA SE tutorial with a simple example using Eclipse, NetBeans and a command line.

1. An easy application of JAVA SE using Eclipse
 

Creating a new project named HelloWorld:
File > New > Java Project > HelloWord > Finish

Creating of the main class in the src directory:
Right click > New > Class
Write a name in the Name input: MyFirstClass
Select public static void main(String[] args) and Inherited abstract methods
Then Finish

It will create a new file named MyFirstClass.java and will contain the following code:

public class MyFirstClass {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub


}


}
 

Let's add the code to display a string:

public class MyFirstClass {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

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


}


}

Let's compile and execute it with the green triangle icon.

In the Console area you will see:

Hello World!

It will create the MyFirstClass.class file in the bin directory.
If you open it, you will see something like that:

// Compiled from MyFirstClass.java (version 1.6 : 50.0, super bit)

public class MyFirstClass {

  

  // Method descriptor #6 ()V

  // Stack: 1, Locals: 1

  public MyFirstClass();

    0  aload_0 [this]

    1  invokespecial java.lang.Object() [8]

    4  return

      Line numbers:

        [pc: 0, line: 2]

      Local variable table:

        [pc: 0, pc: 5] local: this index: 0 type: MyFirstClass

  

  // Method descriptor #15 ([Ljava/lang/String;)V

  // Stack: 2, Locals: 1

  public static void main(java.lang.String[] args);

    0  getstatic java.lang.System.out : java.io.PrintStream [16]

    3  ldc <String "Hello World!"> [22]

    5  invokevirtual java.io.PrintStream.println(java.lang.String) : void [24]

    8  return

      Line numbers:

        [pc: 0, line: 9]

        [pc: 8, line: 11]

      Local variable table:

        [pc: 0, pc: 9] local: args index: 0 type: java.lang.String[]

}

It is the bytecode that the Java Virtual Machine (JVM) will transform into machine code before its execution.

2. An easy application of JAVA SE using NetBeans

This is the same as the Eclipse way.

3. An easy application of JAVA SE using a command line tool

Go until your directory of your project, open a command line.
It supposes that you already created your MyFirstClass.java file.

Then type:

$ /yourPathUntilTheDirectoryProject/> javac MyFirstClass.java

It will create the MyFirstClass.class file.

Let's execute it now and be careful, there is no .class extension at the end of the file:

$ /yourPathUntilTheDirectoryProject/> java MyFirstClass

Result:

$ Hello World!

Good job you made it!

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.