views:

321

answers:

3

I want to launch a browser and load a web page using Java's Runtime exec. The exact call looks like this:

String[] explorer = {"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE", 
    "-noframemerging", 
    "C:\\ ... path containing unicode chars ... \\Main.html"};
Runtime.getRuntime().exec(explorer);

In my case, the path contains "\u65E5\u672C\u8A9E", the characters 日本語.

Apparently it's a java bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4947220

My question is: is there a viable workaround that can be done solely using Java? It appears that it is possible to write a JNI library for this, but I'd like to avoid that if possible. I have tried URI-encoding the path as ascii and writing the commands to a batch file, without success.

A: 

These are the two solutions I considered, each of which are more or less workarounds:

  1. Create a temp html redirect file which will redirect the browser to the proper page. Note that IE will expect unencoded unicode for local files, while other browsers may accept only uri-encoded file paths

  2. Use the short filename for the windows file. It won't contain unicode characters.

Bear
A: 

I think you can use Apache Commons Exec library or ProcessBuilder to give a try;)

israkir
ProcessBuilder seems to have the same trouble with unicode in its command argument string. I unfortunately can't bring in an outside library for my particular case.
Bear
How about initialize your arguments to variables using getPath() method and use them in ProcessBuilder without touching any non-unicode stuff within source code?
israkir
A: 

At the mentioned Java bug page you will find a workaround that is reported to work using ProcessBuilder and wrapping the parameters in environment variables. Here is the source code from Parag Thakur:

String[] cmd = new String[]{"yourcmd.exe", "Japanese CLI argument: \ufeff\u30cb\u30e5\u30fc\u30b9"};        
Map<String, String> newEnv = new HashMap<String, String>();
newEnv.putAll(System.getenv());
String[] i18n = new String[cmd.length + 2];
i18n[0] = "cmd";
i18n[1] = "/C";
i18n[2] = cmd[0];
for (int counter = 1; counter < cmd.length; counter++)
{
    String envName = "JENV_" + counter;
    i18n[counter + 2] = "%" + envName + "%";
    newEnv.put(envName, cmd[counter]);
}
cmd = i18n;

ProcessBuilder pb = new ProcessBuilder(cmd);
Map<String, String> env = pb.environment();
env.putAll(newEnv);
final Process p = pb.start();
mklhmnn