X

Sundararajan's Weblog

JavaFX interactive shell

Guest Author

JavaFX compiler has a built-in script shell - Per Bothner has implemented a read-eval-print loop facility for JavaFX. The script shell class is com.sun.tools.javafx.script.ScriptShell.
Note:This is in the openjfx-compiler repository and not in the JavaFX 1.0 binary.

A sample JavaFX session is as follows:


$ java -cp dist/lib/shared/javafxc.jar com.sun.tools.javafx.script.ScriptShell
/\*fx1\*/ "hello"
hello
/\*fx2\*/ 2 + 4
6
/\*fx3\*/ function greet (name) { println("Hello, {name}") }
/\*fx4\*/ greet("Sundar")
Hello, Sundar
/\*fx5\*/ var s = [ "Sunday", "Monday", "Tuesday" ]
[ Sunday, Monday, Tuesday ]
/\*fx6\*/ for (i in s) println(i)
Sunday
Monday
Tuesday
/\*fx7\*/ class Person { public var name: String; }
/\*fx8\*/ var p = Person { name: "Sundar" }
fx7$Person@13fba1
/\*fx9\*/ p.name
Sundar
/\*fx10\*/ import javax.swing.\*;
/\*fx11\*/ var f = new JFrame("hello");
fx11:1: cannot find symbol
symbol : class JFrame
location: class fx11
/\*fx12\*/
/\*fx13\*/ var f = new javax.swing.JFrame("hello")
javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=hello,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
/\*fx14\*/ f.setSize(300, 300)
/\*fx15\*/ f.setVisible(true)

While there is a generic shell called "jrunscript" for languages that implement jsr-223, it does not work very well for statically typed languages (which can be said about jsr-223 itself). But, the above mentioned JavaFX script shell is independent of jsr-223 interface.

Besides, jrunscript crashes for JavaFX!


$ jrunscript -cp dist/lib/shared/javafxc.jar -l javafx
fx> "hello"
Exception in thread "main" java.lang.NoSuchMethodError: com.sun.tools.javac.main.RecognizedOptions.getJavacFileManagerOptions(Lcom/sun/tools/javac/main/RecognizedOptions$OptionHelper;)[Lcom/sun/tools/javac/main/JavacOption$Option;

at com.sun.tools.javac.util.JavacFileManager.(JavacFileManager.java:973)

at com.sun.tools.javafx.api.JavafxcTool.getStandardFileManager(JavafxcTool.java:102)

at com.sun.tools.javafx.script.JavaFXScriptCompiler.(JavaFXScriptCompiler.java:87)

at com.sun.tools.javafx.script.JavaFXScriptContext.(JavaFXScriptContext.java:45)

at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.getJavaFXScriptContext(JavaFXScriptEngineImpl.java:61)

at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.getJavaFXScriptContext(JavaFXScriptEngineImpl.java:55)

at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.parse(JavaFXScriptEngineImpl.java:220)

at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.eval(JavaFXScriptEngineImpl.java:173)

at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.eval(JavaFXScriptEngineImpl.java:164)

at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)

at com.sun.tools.script.shell.Main.evaluateString(Main.java:280)

at com.sun.tools.script.shell.Main.processSource(Main.java:249)

at com.sun.tools.script.shell.Main.access$100(Main.java:19)

at com.sun.tools.script.shell.Main$1.run(Main.java:165)

at com.sun.tools.script.shell.Main.main(Main.java:30)

This is because JavaFX compiler code depends on modified javac classes. The code for jrunscript lives in tools.jar along with the unmodified javac classes! These unmodified javac classes are loaded which results in exception from javafxc code! We have to use the bootstrap path workaround as shown below:


$ jrunscript -J-Xbootclasspath/p:./dist/lib/shared/javafxc.jar -l javafx
fx> 233 + 334
567
fx> for (i in [0..4]) println(i)
0
1
2
3
4
fx>

But, now we have a much better ScriptShell for JavaFX :-) We don't need to resort to this hack...

Join the discussion

Comments ( 4 )
  • Philippe Lhoste Monday, February 23, 2009

    I suppose it is in the 1.1 release?

    I have:

    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javafx/script/ScriptShell

    with the 1.0 release.


  • A. Sundararajan Monday, February 23, 2009

    Hi Philippe Lhoste: I am sorry - I should have made this explicit. This is in the current code base of openjfx-compiler and not in 1.0 binary.


  • Ankara Bilgisayar Bilgisayar Satis Thursday, February 26, 2009

    Thanks for the nice and explanatory article!


  • Anurag Wednesday, March 11, 2009

    Hi.

    We have been trying to use the JythonScriptEngine in our project. We run jython scripts from within a tomcat container which can potentially spawn 100s of threads to service the requests.

    Now the problem is since this engine keeps ThreadLocal PySystemStates we will end up having 100s of such states (all having the modules cached). Is there any work around for this? We would like to have a single PySystemState for all the threads. Do you think it is possible with this engine?

    Any help on this will be greatly appreciated!!

    thanks,

    -anurag.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.