对于我正在处理的项目,我需要创建一个relauncher,因此它始终使用javac.exe而不是java.exe运行。
代码:
Console.info("Relaunch");
ProcessBuilder builder = new ProcessBuilder();
ArrayList<String> args = new ArrayList();
args.add(java.getAbsolutePath());
URL url = Relauncher.class.getResource("/dk/app/Relauncher.class");
if(url == null){
new CrashReport(new RuntimeException("Relauncher not found"), "Unable to relaunch").execute();
}
if(url.toString().startsWith("jar:file:/")){
Console.debug("relaunch type = jar");
String jar = url.toString().substring(10);
if(jar.contains("!")){
jar = jar.split("!")[0];
}
args.add("-jar");
args.add(jar);
//args.add("dk.app.Main");
}else if(url.toString().startsWith("file:/")){
Console.debug("relaunch type = class");
String cls = url.toString().substring(6);
args.add("-cp");
args.add(cls);
}
builder.command(args);
String cmds = "";
for(String str : args){
cmds = cmds + "\"" + str + "\" ";
}
Console.debug("execute: " + cmds);
final Process p = builder.start();
但是当我构建这个项目并尝试使用java.exe启动它时,它会触发重新启动器。我收到错误:
'Error: Unable to access jarfile C:/Users/steven/nb%20projects/Default_Application/dist/Default_Application.jar'
这是我运行时的完整日志:
[15/02/2014 20:54:31] [Info] Relaunch
[15/02/2014 20:54:31] [Debug] relaunch type = jar
[15/02/2014 20:54:31] [Debug] execute: "C:\Program Files\Java\jdk1.7.0_25\bin\java.exe" "-jar" "C:/Users/steven/nb%20projects/Default_Application/dist/Default_Application.jar"
Error: Unable to access jarfile C:/Users/steven/nb%20projects/Default_Application/dist/Default_Application.jar
[15/02/2014 20:54:31] [Info] Relaunch exit: 1
[15/02/2014 20:54:31] [Error] Failed to relaunch with jdk
我知道这个错误可能意味着jar文件不存在,但我绝对相信它存在,因为我在cmd中尝试了完全相同的命令。在cmd它工作了!