Yab*_*797 4 java variables cmd external path
我正在寻找通过命令行执行外部程序,但我发现如果程序存在于我调用它的目录中,我只能这样做.我希望能够从任何目录执行该程序.
我为windows(7)设置了Path变量,并且能够使用命令行手动从任何目录执行程序; 但是我无法通过Java这样做.
相关代码:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(new String[]{"C:\\AutomateKPI\\GetLog.exe", "-e", rossIP});
Run Code Online (Sandbox Code Playgroud)
我的问题是上述程序的输出产生了一般命名的文件"log.txt".在线程化程序时会出现问题.如果无法使用路径变量,或者我可以将程序复制到新目录中,然后将其删除.我想避免这样做.
编辑:上面的代码适用于GetLog.exe驻留在C:\ AutomateKPI中.我想引用%PATH%,这样我就可以从C:\ AutomateKPI\*NewDir*运行GetLog.exe
尝试使用ProcessBuilder
.它允许您指定工作目录:
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();
Run Code Online (Sandbox Code Playgroud)
或者,如果C:\ AutomateKPI在您的%PATH%
:
ProcessBuilder pb = new ProcessBuilder("GetLog.exe", "-e", rossIP);
Run Code Online (Sandbox Code Playgroud)
从文档中不清楚,但ProcessBuilder
似乎以与系统类似的方式定位事物,例如%PATH%
在Windows上使用.
归档时间: |
|
查看次数: |
2680 次 |
最近记录: |