This post originated from an RSS feed registered with Java Buzz
by Weiqi Gao.
Original Post: New Toys In Java 6: A Follow-Up
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
Now that Java 6 is released, I finally got a chance to play with some of its new features with the help of the documentation, and to fill out some of the questions marks in my earlier post.
(BTW, the project I'm working on may update its Java version from 1.4 to 5.0. Now someone will have to gather the rationale for going to 6.0. If we start the balling rolling, we may get to use Java 6 starting from 2009.)
Command line tools
Looking into the bin directory, we find the following:
[weiqi@gao]$ diff -q -r /opt/jdk1.6.0.amd64/bin /opt/jdk1.5.0_08.amd64/bin | grep Only | sort
Only in /opt/jdk1.5.0_08.amd64/bin: kinit
Only in /opt/jdk1.5.0_08.amd64/bin: klist
Only in /opt/jdk1.5.0_08.amd64/bin: ktab
Only in /opt/jdk1.6.0.amd64/bin: jhat
Only in /opt/jdk1.6.0.amd64/bin: jrunscript
Only in /opt/jdk1.6.0.amd64/bin: schemagen
Only in /opt/jdk1.6.0.amd64/bin: wsgen
Only in /opt/jdk1.6.0.amd64/bin: wsimport
Only in /opt/jdk1.6.0.amd64/bin: xjc
The command line tools kinit, klist and ktab in Java 5 is not present in Java 6.
Of the six new tools introduced in Java 6, four (schemagen, wsgen, wsimport and xjc are Java web services tools, which I probably won't have occasion to use in anything beyond toy programs. The other two, jhat and jrunscript are of greater interest to me.
In my earlier post, I also identified jmap and jstack as new in Java 6. They are actually present in Java 5.
Running scripts
The jrunscript tool is a command line script shell. It is capable of running any JSR 223 compliant scripting engine scripts in an interactive (repl) environment. A watered down version of the Mozilla Rhino JavaScript engine is bundled with Java 6. Many JSR 223 engines are available from https://scripting.dev.java.net/.
I have found the word "Engine" a little confusing in this context because the JSR 223 engine for, say, Jython is not the only thing that is required to run Python scripts in Java 6 programs. To run Python scripts from Java 6 programs, you need both the jython.jar file from http://jython.org and the jython-engine.jar from https://scripting.dev.java.net/.
Another gotcha is that jrunscript does not honor CLASSPATH when searching for script engines. After putting the language jars and the engine jars into my CLASSPATH, here's what I get when I try to list the available engines with the -q command line switch (they spelled "implementation" wrong, quickly, somebody file a bug report):
[weiqi@gao]$ jrunscript -q
Language ECMAScript 1.6 implemention "Mozilla Rhino" 1.6 release 2
[weiqi@gao]$ jrunscript -cp $CLASSPATH -q
Language awk Awk as specified in OpenGroup's single UNIX spec. Version 2 implemention "jawk" 0.14
Language ruby 1.8.4 implemention "jruby" 0.9.1
Language BeanShell 2.0b5 implemention "BeanShell Engine" 1.0
Language ECMAScript 1.6 implemention "Mozilla Rhino" 1.6 release 2
Language python 2.1 implemention "jython" 2.1
Language groovy 1.0 implemention "groovy" 1.0-RC-02
To start jrunscript with a different engine from the default JavaScript engine, use the -l command line switch. Here's an interactive awk session:
[weiqi@gao]$ jrunscript -cp $CLASSPATH -l awk
jawk> /^Warning:.*/ { print $0 }Error: This is an error message.Warning: This is a warning messag.
Warning: This is a warning messag.
Ctrl-D
java.io.PrintStream@1b52513a
jawk>
As repls go, I have to say jrunscript is a bit anemic. For example, I can't define a multiline function in it. Since most of the JVM based scripting languages comes with their own interactive shells, I'll stick with them, and using jrunscript only to make sure that my JSR 223 scripting engines were installed correctly.
Troubleshooting with jhat
The new in Java 6 jhat tool belong to the troubleshooting tools group. It is a heap dump browser. It is used in conjuction with the other tools in the group, such as jps and jmap that were introduced in Java 5:
[weiqi@gao]$ java Foo & # a trivial swing frame
[1] 15558
[weiqi@gao]$ jps
15558 Foo
15573 Jps
[weiqi@gao]$ jmap -dump:format=b,file=Foo.dump 15558
Dumping heap to /home/weiqi/temp/junk/Foo.dump ...
Heap dump file created
[weiqi@gao]$ jhat Foo.dump
Reading from Foo.dump...
Dump file created Tue Dec 26 21:20:39 CST 2006
Snapshot read, resolving...
Resolving 38538 objects...
Chasing references, expect 7 dots.......
Eliminating duplicate references.......
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.
Now I can browse the heap dump at the URL http://localhost:7000/. Here's the initial screen: