4

I'm looking to execute an external program through the command line, but I've found I'm only able to do so if the program exists in the directory I am calling it from. I would like to be able to execute the program from any directory.

I've set the Path variable for windows (7) and am able to execute the program from any directory manually with the command line; however i am unable to do so through Java.

Relevant code:

 Runtime rt = Runtime.getRuntime();

 Process proc = rt.exec(new String[]{"C:\\AutomateKPI\\GetLog.exe", "-e", rossIP});

My issue is that the output of the above program produces a generically named file "log.txt". This will cause problems when threading my program. If it is impossible to use the path variable, alternatively i can copy the program into the new directory and delete it afterwards. I would like to avoid doing so.

Edit: The above code works as GetLog.exe resides in C:\AutomateKPI. I would like to reference %PATH% so I can run GetLog.exe from C:\AutomateKPI\*NewDir*

jww
  • 97,681
  • 90
  • 411
  • 885
Yabo9797
  • 53
  • 1
  • 6
  • What is the error you get, also is the GetLog.exe in the directory c:\automateKPI? – Rocky Pulley Aug 09 '12 at 14:14
  • I'm not getting an error, but I would like to thread my program and execute GetLog.exe in various directories. C:\AutomateKPI\*NewDir*\GetLog.exe without copying GetLog.exe into the newly created directory – Yabo9797 Aug 09 '12 at 14:20
  • yes, your problem is that you are providing a full path to getlog.exe and it doesn't exist in that path. You need to execute getlog.exe in its actual path and then provide a working directory. – Rocky Pulley Aug 09 '12 at 18:31
  • Possible duplicate of [How to set an environment variable in Java using exec?](https://stackoverflow.com/questions/8607249/how-to-set-an-environment-variable-in-java-using-exec) – jww Mar 24 '18 at 07:16

2 Answers2

5

Try using ProcessBuilder. It allows you to specify the working directory:

String commandPath = "C:" + File.pathSeparator +
                     AutomateKPI" + File.pathSeparator + "GetLog.exe";
ProcessBuilder pb = new ProcessBuilder(commandPath, "-e", rossIP);
pb.directory(new File("intendedWorkingDirectory"));
Process p = pb.start();

Or, if C:\AutomateKPI is in your %PATH%:

ProcessBuilder pb = new ProcessBuilder("GetLog.exe", "-e", rossIP);

It's unclear from the docs, but ProcessBuilder appears to locate things in a way that's similar to the system, e.g. using %PATH% on windows.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • Would this be correct? Process proc = rt.exec(new String[]{"exec cmd /c GetLog.exe", "-e", rossIP}) – Yabo9797 Aug 09 '12 at 14:13
  • With ProcessBuilder you can set/alter an environment variable for a Process. – Peter Lawrey Aug 09 '12 at 14:17
  • @pb2q You can use the File.pathSeperator to build the path is a platform independent way. – Peter Lawrey Aug 09 '12 at 14:22
  • I'll take a look into ProcessBuilder. If i want to change the directory that I'm executing GetLog.exe from (using %PATH%) can I do as follows? Process proc = rt.exec(new String[]{"cmd", "/c", "C:\\AutomateKPI\\*NEWDIR*\\GetLog.exe", "-e", rossIP}); Mind that the %PATH% is currently set to C:\AutomateKPI where GetLog.exe resides . – Yabo9797 Aug 09 '12 at 14:24
  • @PeterLawrey thanks, `ProcessBuilder` is the the better solution, seems to work as expected on windows. – pb2q Aug 09 '12 at 14:27
0

Well, as long as you know the path of the program that you're opening, and you dont have to use cmd, this should work every time:

File file = new File("File Directory");
Desktop dt = Desktop.getDesktop();

try {
    dt.open(file);
} catch (IOException e1) {
}
PulsePanda
  • 1,806
  • 10
  • 33
  • 56