### Java Sample HelloWorld

This is a simple Java console application displaying Hello World! on the console.
There are two versions; a Class and a Package with a Class.
Testing has been done on a Raspberry Pi 3 with Java Version 1.8.0_162.
Sharing this example in case a simple Java console application is required.
Class HelloWorld
Prepare
Create a folder HelloWorldConsole and set as default.
$mkdir HelloWorldConsole
$cd HelloWorldConsole
Java Code File HelloWorld.java
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
Java Compile Command Line
Compile the code and list the content of the directory to check if the class file HelloWorld.class is created.
Ensure folder HelloWorldConsole is set.
$javac HelloWorld.java
$ls
HelloWorld.class HelloWorld.java
Java Run Class HelloWorld
$java HelloWorld
Hello World!
Package helloworld with Class HelloWorld
Prepare
Create a folder HelloWorldConsolePackage and set as default.
$mkdir HelloWorldConsolePackage
$cd HelloWorldConsolePackage
Java Code File HelloWorld.java
package helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Java Compile Command Line
Compile the code and list the content of the directory to check if the package folder helloẃorld and the class file HelloWorld.class are created.
Ensure folder HelloWorldConsolePackage is set.
$javac -d . HelloWorld.java
$ls
helloworld HelloWorld.java
$ls helloworld
HelloWorld.class
Java Run Package helloworld with Class HelloWorld
$java helloworld.HelloWorld
Hello World!

Changed: 2018-04-18 10:27, Category: Java, File: T6C205M2158.htm, ID: 2158