Wednesday, April 21, 2010

IcedTea Java Console: How to debug Java Applets on Unix / Linux based systems

If you've ever used the IcedTea Java plugin, you may have noticed that it does not have a console like it's Windows based cousin.

How do you debug or see the Java output without a console? The answer is very simple: If you are using IcedTea it's because you are running a Unix / Linux based system. You have built-in consoles all around you!

THE QUICK SOLUTION: (for the impatient)

Before running your applet in a browser, open a terminal window and type:

$ watch -n 1 'cat $HOME/.icedteaplugin/java.stdout'

Now run your applet and see the output fill up the screen.

THE FULL EXPLANATION:

Open a terminal and go to your icedteaplugin personal settings folder. It should be named .icedteaplugin in your $HOME directory:

$ cd $HOME/.icedteaplugin

Do a directory listing and you will see a few files and a directory.

$ ls

cache
java.stdout
java.stderr


cache is the directory which contains java applets that have been accessed before. If you ever debug an applet you will know that sometimes it's useful to 'clear' the cache. You can individually remove the cached applet that you are debugging, to force IcedTea to download the latest version, by deleting the file.

java.stdout contains standard output from a running java applet, or if none is running it may contain data from the last running applet.

java.stderr contains standard error output.

Now that you know where all the data is, how can you see the output in real time?

Well, you can. At least you can see the output in near real time by using the useful Unix/Linux watch and cat commands.

If you are already in your $HOME/.icedteaplugin directory, type:

$ watch -n 1 'cat ./java.stdout'

If you open another terminal window, type:

$ watch -n 1 'cat $HOME/.icedteaplugin/java.stderr'

Use man watch and man cat for all the optional parameters, but note that the -n option for watch specifies the update rate in seconds.