Chapter 7. Don't reinvent! Use what exists in Java!

Obix source code can always contain any valid Java source code.

In Chapter 2, First example: "Hello world!" we used Obix's instruction executer to display a message on the system console. The screen looked like this:

The same result can be obtained with Java source code contained in Obix source code. The principle is that Java statements just have to be embedded between java and end java. To see how this works, enter the following code in the instruction executer:

Press 'Execute'. The system console displays:

Hello world!

Obix and Java source code can be mixed. For example, the following script uses Obix to execute a loop 3 times, and Java to display a message:

repeat 3 times
   java
      System.out.println ( "Hello world!" );
   end java
end repeat

The result displayed is:

Hello world!
Hello world!
Hello world!

The inverse can be done too: use Java for the loop, and Obix to display the message:

java
   for (int i=1; i<=3; i++) {
end java
      console.message ( "Hello world!" )
java
   }
end java

The interaction of Obix and Java is not limited to inserting Java statements in Obix scripts. Obix code can also be called from any Java applications, because the Obix compiler creates Java .class files. Data can easily be exchanged between Obix and Java, and Obix components can implement Java interfaces and/or extends Java classes.

The easy and seamless integration of Obix and Java has always been one of the main goals in the development of Obix. The obvious benefit is that the huge quantity of available and proven Java software can easily be used from within Obix. It is not difficult to write Obix wrappers for Java classes, so that Obix's features not found in Java (for example Contract programming) can quickly be added, without having to rewrite the 'hard part' of existing software components.

For example, Java's servlet technology for writing professional web applications is fully supported in Obix. Thus, a web application could be composed of:

For more information and further examples see Chapter 10, Embedded Java source code in the programming manual.