0

I want to set a path in my Java program with this Windows command (this path contains some DLL files that are used in a native peripheral of my program):

c:\>path=%path%;"C:\Users\NetBeansProjects\IPTV1.7\3rd_party"

But this causes an exception when the program runs:

java.io.IOException: Cannot run program "path=%path%;C:\Users\NetBeansProjects\IPTV1.7\3rd_party\": CreateProcess error=2, The system cannot find the file specified

I don't know why I can set the path with no problem in the command prompt but then get an exception thrown in the code.

String path = "C:\\Users\\NetBeansProjects\\IPTV1.7\\3rd_party\\";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("path=%path%;"+ path);
Pops
  • 30,199
  • 37
  • 136
  • 151
sajad
  • 2,094
  • 11
  • 32
  • 52
  • When you say native peripheral, what do you mean? At some point you are trying to launch another process from within your java program, and what you are wanting to do is control the path that that launched process gets? – Tom Quarendon Mar 01 '11 at 14:29

3 Answers3

4

Your command

path=%path%;"C:\Users\NetBeansProjects\IPTV1.7\3rd_party"

is not a "real" windows command, but only a variable assignment, interpreted by your shell (cmd.exe), visible only in the same shell session and any commands (other programs) started from there.

When trying to execute this from Java with Runtime.exec(), the windows CreateProcess function tries to find a executable file with this strange name, which obviously does not exist (can't exist, I think), and you get this error.

Even if you could execute this, for example by calling cmd.exe, it would only influence this same cmd.exe process (and any programs started from there, not your own Java process (and programs started from here).

Depending on what you in fact want, you could, for example:

  • give the path with ProcessBuilder directly to the process which in fact needs it (like Aaron showed)
  • search by yourself for executable files, if you want this for finding commands in the next exec
  • put the variable assignment and other commands together in a .BAT or .CMD file and execute this.
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
3

When you type this on the command prompt, the cmd program processes it and changes the PATH variable for you. When you try this with Runtime, no cmd process is created and there is no command "path=%path%;C:\Users\NetBeansProjects\IPTV1.7\3rd_party\" on your harddisk (Windows actually tries to find a program with this exact name).

Put the commands in a .BAT or .CMD file. Windows automatically creates a cmd process to execute them for you.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
3

You can use ProcessBuilder in java to spawn a process and control the environment that it gets. So you would use the ProcessBuilder environment method to set the PATH environment variable, then set the relevant command line, then launch. SO something like (untried):

    ProcessBuilder b = new ProcessBuilder();
    b.environment().put("PATH", whatever);
    b.command(whatever);
    Process p = b.start();
Tom Quarendon
  • 5,625
  • 5
  • 23
  • 30