Fun with JavaFX on Ubuntu Linux
A friend of mine got me interested in JavaFX recently and my interest grew when I discovered that it had some nice charting functionality. Dean Iverson has written some great tutorials on the subject over at his blog and includes a link to a demo showing some of the different plot types that are available.
The demo is called ChartDemo and can be found here
http://pleasingsoftware.blogspot.com/2009/06/this-is-test.html
In an ideal world you simply have to click on the demo’s screenshot for it to download and launch but that wasn’t what happened for me. What happened when I clicked on it was nothing. No error messages…just nothing. It’s difficult to google ‘nothing happened’ and get something useful so I downloaded the demo (which had the filename ChartDemo.jnlp) and tried to launch it from the command line using
javaws ChartDemo.jnlp
This gave me the error message
netx: Unexpected net.sourceforge.jnlp.ParseException: Invalid XML document syntax. at net.sourceforge.jnlp.Parser.getRootNode(Parser.java:1200)
What follows is the story of how I eventually got this demo to work in the hope that it will help someone out there.
So, first things first, what are some of the relevant system specs I am using? Well, I am running 32bit Ubuntu Linux 9.10 (Karmic Koala) and
java -version
gives
java version "1.6.0_0" OpenJDK Runtime Environment (IcedTea6 1.6.1) (6b16-1.6.1-3ubuntu1) OpenJDK Server VM (build 14.0-b16, mixed mode)
Now, when I googled the error message I discovered that Linux (more specifically, I guess, the OpenJDK) is much more sensitive to xml errors than Windows/Mac OS X (.jnlp files are written in xml). Take double quotes for example; according to the W3C XML recommendations you should not use \” inside an xml attribute but should use “"” instead. Some java implementations don’t seem to care but, at the time of writing at least, OpenJDK definitely does. Follow this link to see the original discussion thread where I learned this.
The practical upshot of this extra level of strictness is that .jnlp files that work just fine on Windows and Mac OS X won’t work on Linux and I guessed that was what as happening here. Sadly there were no examples of \” in ChartDemo.jnlp for me to change to “"” so there must be something else ‘wrong’ with it; but what?
I decided to try the ‘stare at it until you figure it out’ approach to debugging and left the laptop on the side of the sofa while watching a movie on TV. About halfway through the movie, inspiration struck and I changed the line
<update check="background">
to
<update check="background"/>
which got things past the xml parsing stage. Sadly, I then hit another problem. Rather than a working ChartDemo, my efforts were rewarded with nothing more than just a blank window and a load of java errors in the terminal. When I say ‘a load’ I mean HUNDREDS and none of them looked particularly illuminating. I was starting to remember why I had avoided Java in the past but was not about to give up so easily.
Let’s take stock:
- The .nlbp file was fine (or at least didn’t return any parse errors)
- The ChartDemo code must be bug free because if it wasn’t then the author would have been told so rather quickly in the comments section of his blog
- My Java setup was presumably fine since I was able to run other JavaFX examples. For example I successfully worked through a JavaFX programming tutorial on Sun’s website without incident.
Of those three points I figured that the third one was the most likely to be wrong. It was OpenJDK’s handling of the .jnlp file that caused my first problem so maybe it was causing this second problem too. Could I switch from using OpenJDK to a different version of Java I wondered? Some googling ensued and I discovered some useful incantations.
I can list the versions of Java installed on my machine with the command
sudo update-java-alternatives -l
to get
java-6-openjdk 1061 /usr/lib/jvm/java-6-openjdk java-6-sun 63 /usr/lib/jvm/java-6-sun
I can change from the openjdk to sun-java with
sudo update-java-alternatives -s java-6-sun
Once I did this I tried to run the ChartDemo.nlbp file again:
javaws ChartDemo.jnlp
and it worked perfectly. I was rewarded with a very nice demo of JavaFX’s charting functionality and Dean’s tutorials proved to be very useful to me. So useful in fact that I bought his book.
Incidentially, the java-6-sun version of java doesn’t care about the syntax of the .jnlp file quite so much as openjdk. However, if you want to change back to using openjdk you can do
sudo update-java-alternatives -s java-6-openjdk
I hope this little tale helps someone out there. Let me know if it does and also feel free to let me know if I have got anything wrong. My knowledge of all things Java is rather basic at the moment to say the least – something I am trying to change.
I liked the darker design.
Since this is top 1 result on google for this error, i will add this :
If one of the XML argument is an URL with some GET parameters
(lets say something like http://www.walkingrandomly.com/?p=2218&another=1) it will also crash.
You cannot use a simple &, you will need to use it’s entity to be
“strictly valid”, which is &. For example the URL above would become :
http://www.walkingrandomly.com/?p=2218&another=1
openJDK will crash on this otherwise, since it waits for an entity code after the & and finds none.
Hope this helps someone else.
If you get this error and dont know why your XML isn’t valid,
just use the validator! http://validator.w3.org/
My jnlp is W3C valid, but i got still this error on windows! With Linux all went well (sun java)