J-CLOPS logoTWI-JUG logo

J-Clops tutorial - Hello world

We start with a simple program HelloWorld which prints the line Hello world! and accepts a single option --times which allows the user to repeat this message as many times as he pleases. Hence, if we call the program without command line options, the result is a single line, as follows:

c:\> java HelloWorld
Hello world!
            
and if we add a command line option, we see the following result
c:\> java HelloWorld --times 3
Hello world!
Hello world!
Hello world!
            
and incidentally, we could also have written
c:\> java HelloWorld --times=3
            
using an equals sign to join the numeric argument to its option.

The Java code for the program is itself very simple:

import be.ugent.twijug.jclops.CLManager;
    
public class HelloWorld {
    
    private int times = 1;

    public void setTimes(int times) {
        this.times = times;
    }
    
    public void run () {
        for (int i=0; i < times; i++)
            System.out.println("Hello world!");
    }
    
    public static void main(String[] args) {
        HelloWorld myProgram = new HelloWorld();
        CLManager clm = new CLManager (myProgram);
        clm.parse(args);
        myProgram.run ();
    }

}
            
We shall discuss the code in more detail on the next page, but already allow us to point out that apart from two lines in the main method, the code itself looks very ordinary. Nowhere do we directly refer to the name "--times" of the option. Indeed, the system infers this name automatically from the fact that there is a setter method called setTimes. (There are no 'hidden' configuration files.)