StokeBloke.com

Java Runtime and cmd.exe unicode issue

For many years our product used the following code to open URLs or files on Windows 2000 and XP.

final String[] cmds = { "cmd.exe", "/C", "start", "/B ", "/WAIT", url };
Runtime.getRuntime().exec(cmds);

This code is really simply and works well. If you have different editors configured for a specific file type the start launches the correct viewer/editor. It also opens the users preferred web browser too.

We didn’t have many issues with this until we started added more localisations to our product and supporting unicode fully.

This code in theory looks good, but it didn’t work in many cases. I found that the cmd.exe process on a European Windows system runs in ISO-8859-1 so any arguments we passed it get completely messed up.

This means the cmd.exe cannot be used as it doesn’t support unicode.

I spent days looking for another alternative but couldnt find a java only fix.  In the end we had to use JNI and use the Windows ShellExecuteExW and ShellExecuteEx calls (based on whether we used a unicode or none unicode string)

One Response to “Java Runtime and cmd.exe unicode issue”

  1. Thomas Says:

    Java has a builtin feature to open, edit, or print URL or files. Its platform independent too.

    All of these new features are contained in the java.awt.Desktop class.

    http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html

Leave a Reply