Java

Thursday, April 12. 2012

Workaround for borderless Java Swing menus on Linux

Since years Linux users are plagued by a bug which affects the rendering of Swing popup menus. When using a modern GTK theme (Like Gnome's Adwaita or Ubuntu's Ambiance and Radiance) then menus in Java Swing applications have no borders and no separators:

Several bug reports exists (Like #6925412) but it seems to be unclear if this is a bug in the affected GTK themes or a bug in Java's GTKLookAndFeel class. Fact is: I'm pretty sure this bug will not be fixed anytime soon so we have to find a workaround and in this article I will show you two different approaches.

Continue reading "Workaround for borderless Java Swing menus on Linux"

Thursday, February 9. 2012

How to implement a Single Instance Application in Java

Recently I started processing command-line arguments in one of my Java Swing applications (Xadrian) so the application can load files directly on startup. This allows connecting the application with some mime-type or file extension and load files into the application by double-clicking the files. But unfortunately this isn't really usable because everytime a new instance of the application is launched by the operating system. So I searched for some technique to prevent this and found two articles: The first one explains how to use a lock file to prevent a second application instance while the second one explains how to use a local server socket to do the same.

But both solutions are not enough for what I want to do. When I double-click some data file and the operating system tries to open this file with a second instance of the application then I want the data file to be opened with the first instance instead. So the second instance must give the command line arguments to the first instance.

I haven't found a library which already does this so I had to write my own library.

Continue reading "How to implement a Single Instance Application in Java"

Saturday, January 28. 2012

How to use Java applets in modern browsers

Finding out how to embed a Java applet in current major browsers is quite a task. Lots of pages still suggest using the <applet> tag and describe how to use the mayscript attribute to allow communication between Java and JavaScript. Other pages recommend using this mysterious mayscript attribute even in <embed> and <object> tags. I think there is a lot of voodoo out there, attributes which sound important but are not necessary at all...

So I started studying this stuff by myself. My goal was embedding a Java applet on a page which must work in Internet Explorer 7+, Google Chrome, Firefox, Opera and Safari. The applet must receive an applet parameter and must communicate with JavaScript in both directions.

Continue reading "How to use Java applets in modern browsers"

Sunday, April 11. 2010

Java and the StartSSL CA certificates

Again and again I forget how to import the StartSSL CA certificates into Java. Everytime when I switch to a different workstation or install a new Linux distribution I can no longer access my StartSSL secured server with Maven. Then I have to search for a tutorial and for the download locations of the CA certs. Very time-consuming. This must stop once and for all. So I wrote a small script which imports the certs into the currently active Java installation.

Steps to install the certs:

  • Download import-startssl script.
  • Make sure JAVA_HOME environment variable is set correctly.
  • Run the import-startssl script.

The script runs the keytool program of Java with sudo so you have to enter your password to give it root access. If you have JSSE installed then the StartSSL CA certs are also added to the jssecacerts keystore. The script imports the root CA certificate and the four sub CA certs (Class 1-4).

If the script does not work for you (Maybe because you are using Windows or Mac OS X instead of a real operating system) then you can at least read it for instructions how to do it manually.

Wednesday, June 3. 2009

Events in Java (Second try)

Well, I'm not that satisfied with the Signal/Slot system I described in a previous article. The reflection part of it makes me itch. So I gave it a second try...

First of all lets summarize the problems with the listener pattern:

  • Two classes (The data container for the event and the listener interface) must be created for each event.
  • At least three methods must be created for each event (Register, Unregister and Fire) in each class which is going to fire the event

So it's simply too much boiler-plate code and the goal is to put as much as possible of it into some library classes without losing any type-safety and without using reflection. I'm going to describe my solution in this article and again I will use the nonsense example of a citizen which drinks some beverage and a Big Brother which monitors it.

Continue reading "Events in Java (Second try)"