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*

4
  • What is the error you get, also is the GetLog.exe in the directory c:\automateKPI? Commented Aug 9, 2012 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 Commented Aug 9, 2012 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. Commented Aug 9, 2012 at 18:31
  • Possible duplicate of How to set an environment variable in Java using exec? Commented Mar 24, 2018 at 7:16

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Would this be correct? Process proc = rt.exec(new String[]{"exec cmd /c GetLog.exe", "-e", rossIP})
With ProcessBuilder you can set/alter an environment variable for a Process.
@pb2q You can use the File.pathSeperator to build the path is a platform independent way.
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 .
@PeterLawrey thanks, ProcessBuilder is the the better solution, seems to work as expected on windows.
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) {
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.