Monday Feb 23, 2009

JavaFX interactive shell

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...

About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder