【发布时间】:2011-09-19 19:14:02
【问题描述】:
我想通过 Runtime 启动 Java jar,但 Windows 再次出现问题。 我知道这一定出现了数百次,但我尝试了一些东西(比如 String.replace("/","\")),由于我无法在 Windows 上调试,这需要一些时间。 这在 Unix 下可以正常工作:
public boolean run(String args[], String workingDir, boolean output) throws
FileNotFoundException {
if (args.length <= 0) {
System.err.println("No cmd provided");
}
if (workingDir == null) {
workingDir = "./";
}
ProcessBuilder pb = new ProcessBuilder(args);
pb.directory(new File(workingDir));
Process p;
int exitValue = 0;
try {
p = pb.start();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
p.waitFor();
if (output) {
while ((line = br.readLine()) != null) {
setChanged();
notifyObservers(line);
}
}
exitValue = p.exitValue();
} catch (InterruptedException ex) {
return false;
} catch (IOException ex) {
return false;
}
if (exitValue == 0) {
return true;
} else {
throw new FileNotFoundException();
}
}
发布者:
public static void launchApp(String subPath) throws FileNotFoundException {
String[] args = String.format("java -jar -Xdock:name=AppName -Xdock:icon=%sicon.icns %AppName.jar", subPath, subPath).split(" ");
ExecRuntime.run(args, null);
}
【问题讨论】:
-
你的问题/问题是什么?
-
也许 java 命令不在该 Windows 机器上的 %PATH% 中?并且至少在这些异常中记录信息,如果发生异常,它们可能会准确地告诉你出了什么问题。
-
我得到一个 FileNotFoundException
-
对,所以它可能找不到java可执行文件,你需要弄清楚它在哪里并提供它的完整路径。
-
那我为什么要先启动App1.jar呢?