Skip to main content

JavaFX Script Engine Factory

5 replies [Last post]
chrono_legionair
Offline
Joined: 2007-12-21
Points: 0

Hi,

I am just wondering with the following codes:

... ...
ScriptEngineManager manager = new ScriptEngineManager(loader);
ScriptEngine engine = engine.getEngineByExtension("fx");
... ...

How is the engine instantiated without first register the extension with the manager? (I think the "fx" extension does not exist out of the box?)

Is there a way to programmatically register a javafx script factory? Something like,

manager.registerEngineExtension("fx", javafxFactory);

If so, how do I get an instance of the factory?

This may sound like a stupid question, I am just experimenting with different ways to bundle my application. It all works well when I have all the jar files (Filter, swing, javafxrt, etc...) located somewhere on the web-server that is publicly accessible.

However, when I try to do something "fancier" where I tried to write a plugin module for another application (the application has the ability to serve contents even if it is in the WEB-INF/lib directory), I get NullPointer with the engine, seems to me that for whatever reason, it is unable to "access" the javafxrt.jar file.

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pforhan
Offline
Joined: 2003-08-07
Points: 0

This is all normally loaded through the magic of JSR-223. It uses the JAR Service Provider specification to do the registration: http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#Service%20Provider

This won't work under OSGi, and some other cases, and at those times, you can directly load net.java.javafx.jsr223.JavaFXScriptEngineFactory and register it or just use it directly.

Pat.

sjoyner
Offline
Joined: 2007-06-13
Points: 0

Please post the full command string you are using to run this program.

Your JavaFX libraries must be on the classpath in order for this code to run.

megasween
Offline
Joined: 2007-11-25
Points: 0

I am using netbeans, and I did not include the JavaFXUserLib in the libraries. Now it works.

megasween
Offline
Joined: 2007-11-25
Points: 0

I just found this. It has a different but "unsupported" way. Im not sure if you can pass values back and forth.

http://jfx.wikia.com/wiki/Programmatically_invoking_a_JavaFX_Script

megasween
Offline
Joined: 2007-11-25
Points: 0

I am having the same problem, so I did some playing around.

I took the following code from an example in the wiki. It has been modified to see what scriptEngineFactories I had. It does not look like I have a javafx script engine. I am usering Java SE 1.6.0. I am able to run JavaFX applications on the system.

The below code checks the script engine manager to see what factories it has. it comes up with just one engine.

"Engine Name: Mozilla Rhino / Engine Language: ECMAScript"

It would be cool if there was a way to get the engine through imports or somthing, but I have not found anything.

The below code fails when it trys to validate the script.
-------------------------------------------------------
package fxexamples;

import java.util.Iterator;
import java.util.List;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;
import javax.swing.SwingUtilities;

public class RunFX {

public static void main(String[] args) {
final DataObject myObj = new DataObject();

Runnable fxScript = new Runnable() {
public void run() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
ScriptEngineManager manager = new ScriptEngineManager(loader);

//////////Start my code
List factories = manager.getEngineFactories();
Iterator factIter = factories.iterator();
ScriptEngineFactory fact = null;

////////////////on my system this loop prints:
//"Engine Name: Mozilla Rhino / Engine Language: ECMAScript"
while(factIter.hasNext()){
fact = (ScriptEngineFactory)factIter.next();
System.out.println("Engine Name: " + fact.getEngineName() +
" / Engine Language: " + fact.getLanguageName());
}

//gets the only script ScriptFactory I have
ScriptEngine engine = fact.getScriptEngine();
//////End my code

Bindings bindings = engine.createBindings();
//Bind the Java Object to the one we'll use in the JavaFX Script

bindings.put("JAVAOBJ:fxexamples.DataObject", myObj);

ScriptContext context = new SimpleScriptContext();

context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

engine.setContext(context);

String script = "import fxexamples.FXtoJava;";

try {
//////////////Code fails here
engine.eval(script);
} catch(Exception ex) {
ex.printStackTrace();
}
}
};

SwingUtilities.invokeLater( fxScript );
}
}

----------------------------------
Here is the output im getting from Run in netBeans:

init:
deps-jar:
Compiling 1 source file to C:\JavaApps\fxexamples\build\classes
compile:
run:
Engine Name: Mozilla Rhino / Engine Language: ECMAScript
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: syntax error (#1) in at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at fxexamples.RunFX$1.run(RunFX.java:53)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
BUILD SUCCESSFUL (total time: 1 second)