Java Programming Tutorials

Java programming tutorials with many code examples!

Java “Hello world!”

Problem:

How to write “Hello, world!” in Java?

Solution:

  1. Create a package for a class:
    mkdir -p com/farenda/solved
    
  2. Create a file com/farenda/solved/HelloWorld.java with content:
    package com.farenda.solved;
    
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, world!");
        }
    }
    
  3. Create directory for compiled classes:
    mkdir classes
    
  4. Compile source:
    javac src/com/farenda/solved/HelloWorld.java -d classes
    
  5. Run compiled code:
    java -cp classes com.farenda.solved.HelloWorld
    

Result:

Hello, world!
Share with the World!