|
|
Athena > Documentation > Scripting |
Scripting
The version 6 of Java Standard Edition allows a tight
integration of scripting languages through a standard API. Athena
supports this interface, scripts written in Smalltalk may be embedded
in programming written in Java 6.
Generally, script engines
for the Java 6 scripting API evaluate well-formed expressions and
statements. Unlike most scripting language based on this API, Athena is
not directly interpreted but compiled and executed by its embedded
virtual machine.
Note that Java 6 is mandatory to execute the code in the remaining of this page.
Evaluating Smalltalk expressions:
After having downloaded the Athena distribution and properly set up your CLASSPATH variable, your Java program may fully enjoy interacting with Smalltalk. You also need to make sure that an image (which has to be named image) is located in the current directory.
First of all, you need to use the proper import statements:
import athena.engine.*; import javax.script.ScriptEngine; import javax.script.ScriptException; import javax.script.ScriptEngineFactory;
Note that if you are using a Java runtime below to 1.6, you need the provided scripting.jar file.
An Athena engine has to be created. This can be done with this snippet of code: ScriptEngineFactory factory = new AthenaScriptFactory(); ScriptEngine engine = factory.getScriptEngine();
Within a try-catch block, eval(String) may be employed to evaluate Smalltalk expressions. try { System.out.println(engine.eval("3 + 4")) } catch(ScriptException e) { System.err.println("ERROR :" + e); }
This should print the result you might expect.
Accessing Java Object within Smalltalk:
Java classes may be instantiated and methods are invokable within Smalltalk. The class System provides the necessary for this: System getJavaClass: 'java.lang.Object'
Printing the result should be similar to 'JavaObject<class java.lang.Object>'
The class JavaObject is a Smalltalk façade for a Java object. The code of this class may be browsed online.
The method invoke: may be used to invoke a Java method: (System getJavaClass: 'java.lang.Object') invoke: 'getClass'
Static method may also be invoked: (System getJavaClass: 'java.lang.System') invokeStatic: 'currentTimeMillis'
prints something like 'JavaObject<1191847743036>'
Related Links:
If you wish to know more about Scripting facilities of Java 6: Interfacing Smalltalk and Java:
|
|
|
|
|