Chapter 3. How to execute Java code in a script?

Besides Obix source code, any script in Obix can also contain Java source code.

To embed Java source code in a script, simply insert the Java statements between a java line and an end java line, as in the following example:

java
   System.out.println ( "Hello world" );
end java

To execute the above Java code, just proceed as explained in the previous chapter.


You can also mix Obix and Java code in a script like this:

// loop with Obix; display with Java
repeat 3 times
   java
      System.out.println ( "Hello world from Java" );
   end java
end

// loop with Java; display with Obix
java
   for ( int i = 1; i <= 2; i++ ) {
end java
      system.console.write_line ( "Hello world from Obix" )
java
   }
end java

Executing the above script will display:

Hello world from Java
Hello world from Java
Hello world from Java
Hello world from Obix
Hello world from Obix

The facility to easily mix Obix and Java source code is one of the most powerful features of Obix, because it enables you to directly use any existing Java software in your Obix application.

For more information and examples to use the standard Java libraries or any .jar files, and writing Obix wrappers for existing Java components, please refer to Chapter 15, Embedded Java source code in Obix's programming language manual.


So far we have seen how to write and execute small snippets of source code. This can be useful, but it's not enough. In the next chapter we are going to see how to develop 'real' applications.