Error when using Runtime.getRuntime().exec method to invoke an exe, if exe path has non-english chars

StackOverflow https://stackoverflow.com/questions/4105280

  •  29-09-2019
  •  | 
  •  

Question

I am using Runtime.getRuntime.exec() Method to invoke an exe. The problem what I face with this method is that when I pass some exe path (c:\JPN_char_folder\mypath\myexe.exe) with other language chars (ex.Japanese) "it's saying "System cannot find the file specified". Would you please suggest some ideas to get around this? I even tried passing that exe path after converting to UTF-8 as well, but still I could not solve this.

-Robert.

Was it helpful?

Solution

I don't think that Japanese characters are the issue; it's the c: drive.

You need to write it this way:

String path = "c:\\\JPN_char_folder\\mypath\\myexe.exe";

See if that helps.

OTHER TIPS

Most probably you have an encoding problem somewhere.

There are several steps here that the path value takes:

  • InstallAnywhere retrieves the path
  • InstallAnywhere puts it into a variable
  • Java reads the variable
  • Java puts it into a String
  • Java creates a java.io.File instance from String
  • Java runtime passes path (via File) to OS

Somehwere along this sequence something goes wrong with the path :-(.

It's hard to tell where; your best bet probably is to try and print out the value at every step along the path, to see where it goes wrong.

At least from inside Java, you should probably print out the String both as text, and as a list of Unicode code points (using String.codePointAt). That way you can see the real data Java uses.

Another approach:

  • Print out the value Java gets from InstallAnywhere (as text & as codepoints, as above)
  • Try to put the path into your Java program as a String literal, and fiddle until you can open the file that way. Then print that String as well.

Now you can compare the two results; that should give you an idea where the path gets messed up. Note:

Does the path contain characters outside the Basic Multilingual Plane (BMP)? Java handles these a bit awkwardly, so you need to pay extra attention. Maybe you can check this first.

Even if you're using Windows, you can use slashes when specifying directories. This will help you with escaping backslash hell.

For example, on my system, 7z is located in directory c:\Program Files\7-Zip\.

Executing this

File file = new File("c:/Program Files/7-Zip/7z.exe");

if(file.exists()) {
    System.out.println(file.getAbsolutePath());
}

Results in

c:\Program Files\7-Zip\7z.exe

being printed on the console.

I'd suggest you try using this idiom, i.e. check if .exe file exits before trying to execute it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top