Skip to main content

JavaFX Script Engine Factory

5 replies [Last post]
Joined: 2007-12-21


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.


Reply viewing options

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

This is all normally loaded through the magic of JSR-223. It uses the JAR Service Provider specification to do the registration:

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


Joined: 2007-06-13

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.

Joined: 2007-11-25

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

Joined: 2007-11-25

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

Joined: 2007-11-25

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"
fact = (ScriptEngineFactory);
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);


String script = "import fxexamples.FXtoJava;";

try {
//////////////Code fails here
} catch(Exception ex) {

SwingUtilities.invokeLater( fxScript );

Here is the output im getting from Run in netBeans:

Compiling 1 source file to C:\JavaApps\fxexamples\build\classes
Engine Name: Mozilla Rhino / Engine Language: ECMAScript
javax.script.ScriptException: syntax error (#1) in at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(
at com.sun.script.javascript.RhinoScriptEngine.eval(
at javax.script.AbstractScriptEngine.eval(
at fxexamples.RunFX$
at java.awt.event.InvocationEvent.dispatch(
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(
BUILD SUCCESSFUL (total time: 1 second)