0

I am trying to launch a .exe file through a Java program. I used the following code:

System.out.println("Opening " + path);
Process exec;
exec = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + path);//path is the path of the exe file which is passed as an argument from another java class

the output is as follows:

Opening C:\Program Files (x86)\C-Free 5\CppIDE.exe

But it is not opening.

Instead when I try

String pat="C:\\Program Files (x86)\\C-Free 5\\CppIDE.exe";
Process exec;
exec = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + pat);

the program is opened.

I don't know what the problem is.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
salabh
  • 1
  • 2

2 Answers2

3

It's very likely that the space in your path is the problem.

I suggest you pass the arguments as an array instead of passing a single string containing the whole command (alternatively you could quote the spaces correctly, but that's not quite as easy).

Either

With ProcessBuilder this could look like this:

ProcessBuilder pb = new ProcessBuilder("rundll32", "SHELL32.DLL,ShellExec_RunDLL", path);
Process p = pb.start();

Also, I see no reason to invoke rundll32 at all in this scenario. This should work just as well:

ProcessBuilder pb = new ProcessBuilder(path);
Process p = pb.start();
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • The program uses Sockets. The file path in a MySQL database at the Server side. It is read and send to the client via a ServerSocket. I tried printing both 'path'(file path received from Server) and 'pat'(file path given in the program) and both are same. However when i use `Process exec;` `exec = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + path);` The program (CppIDE) is not opening. – salabh Nov 17 '12 at 02:53
0

You need to construct the path using File.separator. The path separator you are using will, in this case, will be system dependant.

NiranjanBhat
  • 1,812
  • 13
  • 17
  • Doesn't really matter, since `rundll32` is already *pretty* system dependent and every system that has that executable will use \ as the file separator. – Joachim Sauer Nov 16 '12 at 08:33