How To Fix The FXEvaluator On Java FX 1.2 and JDK 1.6 📎
A simple invocation of the FXEvaluator:
var output = FXEvaluator.eval("{2+3}");
println(output);
Will cause the following error on JavaFX SDK 1.2 and JDK 1.6 (Mac OS X):
java.lang.NoClassDefFoundError: com/sun/tools/javafx/script/JavaFXScriptEngineFactory
at javafx.util.Evaluator.eval(FXEvaluator.java:33)
at javafx.util.FXEvaluator.eval(FXEvaluator.java:88)
at dynamicfx.Main.javafx$run$(Main.fx:17)
at dynamicfx.Main.javafx$run$(Main.fx:17)
It seems like the JSR-223 runtime is not in the JavaFX classpath. Only JDK 1.5 classes are included in the compilation and runtime classpath. Including JDK 1.6 classes directly to the JavaFX-runtime will cause another runtime-error. It seems like the Java FX SDK comes with a modified javac compiler.
To fix the problem, just prepend the script-api.jar and the javafxc.jar to the bootclasspath. This is easily possible in the file ${javafx_home}/profiles/desktop.properties. Just change the entry from:
# execute_bootclasspath_prepend=
to:
execute_bootclasspath_prepend="${javafx_home}/lib/shared/javafxc.jar;${javafx_home}/lib/desktop/script-api.jar"
You don't have to change anything in the IDEs. It just works in netbeans 6.5.1 FX and eclipse 3.5. After this fix, you will be even able to evaluate Java FX dynamically inside the already dynamic script :-):
var output = FXEvaluator.eval("{2+3 + (javafx.util.FXEvaluator.eval('{2}') as Number)}");
println(output);
Moving the whole value of the execute_classpath into the execute_bootclasspath_prepend with both jars (see above) prepended fixes the problem as well.