Beginning Java for Xojo Programmers III

Thanks to Norman we have the Hello World Tutorial in Part II of the Tutorial. So I thought it may be good to have the possibility to control in the command line when the System will say Hello World.

public class Main {

    public static void main(String[] args) {
    		System.out.println("Hello World");
	    }
}

That was the first Tutorial. I will give us the chance to decide from the command line, if Hello World is printed. That we do with the If command:

public class Main {

    public static void main(String[] args) {

    	if (args[0].equals( "Palardys_Tutorials_are_nice" )){

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


    }
}

So what this Code is doing? The Variable args contains the command line args. this command line args are the arguments written in command line when starting the Program. The Line

```
if (args[0].equals( "Palardys_Tutorials_are_nice" ))
```

is looking if the command line arguments in the first position of the array (caution that is zero based) is equal to “Palardys_tutorials_are_nice”.

This is like written here working. So if we would compile the Program after editing like described in the Tutorial II with this command:
java helloworld Palardys_Tutorials_are_nice
we would get our Hello World as Result.

But what if somebody now uses the Program without arguments? That would result in an error message. We enter the Array on Position 0. If it is empty the System complains about it:

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0.

So we have to find security for it: the user shall be able to use the Program with or without the command line args. To do that we have to take care about. So we use a second if condition for it.

if (args.length > 0) {

looks if the args Array has more then one entry. If not, the Hello World Program says nothing.

Now our Program looks like this:

public class Main {

    public static void main(String[] args) {
if (args.length > 0) {
	if( args[0].equals( "Palardys_Tutorials_are_nice" ) ) {

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

    }
}

And how would it look in Xojo? Here’s the Code in Xojo:

if args.count > 1 then
  
  if args(1)="Palardys_Tutorials_are_nice" then
    
    print("Hello World")
    
  end if
  
  
end if

One difference: in Java there is the First Command Line Arg on Position 0 in the Array!

3 Likes

Shouldn’t that be

public class HelloWorld {


}

or am I misunderstanding something here?

Do what you want. I do most times that way cause it makes it fast to find the start class. I can give that class all names I want.
So in that way I have that class the Name Main. I could also say anything else.

From part II:

Save this in a file named HelloWorld.java âƒȘ That EXACT name - java is particular that way




In Java the runtime looks for the MAIN method and invokes that. But how does it know which main method to invoke ? Recall that the command we used was

java HelloWorld

which tells the java runtime to load the HelloWorld compiled class and invoke the main method in there

First of all: Norma described that already. Second: when working with Netbeans the IDE asks for the main method and the target class so user can select. In IntelliJ IDE you can automatically search for the main method (public static void main
).

Thorsten, mir ist klar daß Norman das beschrieben hat. Une es mag DIR klar sein, aber einem AnfĂ€nger wie MIR ist es nicht.

Norman sagt daß die Methode main in der Klasse HelloWorld sein muß damit Java sie dort sucht wenn es das Programm HelloWorld started.

Du benennst die Klasse einfach um ohne zu erklÀren warum. Also schreibt einer von euch beiden Unsinn.

Ihr seid beide VIEL bessere Programmierer als ich - aber wie bei so vielen Profs seid ihr auch betriebsblind fĂŒr die Probleme von AnfĂ€ngern, einfach weil “es” euch so klar ist daß ihr es gar nicht mehr hinterfragt 
 und damit Schritte ĂŒbergeht die ein AnfĂ€nger nicht einfach auslassen kann wenn er es verstehen will. Es hat schon seinen Grund warum meine Artikel fĂŒr Beginner in xDev so viel Arbeit machten 


1 Like

You are right.

Java needs for starting a jar file the position of the main method to start the program itself. In the main method the Java program has it’ s event dispatching, there we describe what we want to do. In the case below it is printing Hello Markus ob Screen.

class Whichnameever{

public static void main(String args){

System.out.println(“Hello Markus”);

}
}

Starting this class directly would print Hello Markus on Screen. But we want to deliver this great Software to our Customers. While we want it to run cross platform we want to produce a jar File. And our IDE (Netbeans or IntelliJ) needs also to know where to start. Therefore we can define the starting method. Clicking in IntelliJ on image opens a menu where to define the main methods place:


After selecting the main method you can run your Application.

The Main Method has to be in one of the classes. You can define even also in every class it’s own main method (I am doing that sometimes). The one you selected there is the one which runs when what run button of the IDE is pressed.

The same is in Artifact settings, also there you need to setup the main method for starting the Application with java jar application.jar .

1 Like