所以我正在研究跨平台引导程序,该程序在OSX / Linux上正常工作,但在Windows中返回以下错误消息:
错误:无法创建Java虚拟机。 错误:发生了致命异常。程序将会退出。 无法识别的选项:-cp C:/myapp/realm/bin/Launcher.jar; C:/ myapp / _jvm / jre /../ lib / tools.jar; C:/ myapp / realm / shared / lib / jetty-util -8.1.4.v20120524.jar
如果我打印出我的ProcessBuilder Arguments列表并在命令行上运行这些参数,它将成功运行。命令应该是
C:/ myapp / _jvm / jre \ bin \ java -javaagent:C:/myapp/realm/bin/spring-agent.jar -cp C:/myapp/realm/bin/Launcher.jar; C:/的myapp / _jvm / JRE /../ LIB / tools.jar中; C:/myapp/realm/shared/lib/jetty-util-8.1.4.v20120524.jar -Xmx256m -Xms200m -Xss128k -XX:+ UseBiasedLocking -XX:+ UseNUMA -DprocessType = realm -Dcert.framework.license = C:/myapp/realm/license/my.license -Djav a.security.policy = C:/myapp/realm/security/java.policy -Djava.security.properties = C:/myapp/realm/security/java.security -Djava.secur ity.auth.login.config = C:/myapp/realm/security/jaas.conf -Drealm.home = C:/ myapp / realm -Duser.dir = C:/ myapp / realm / bin -Djava.endorsed.dirs = C:/ myapp / realm / endorsed; C:/ myapp / _jvm / jre / lib / endorsed -Djava.ext.dirs = C:/ myapp / realm / ext; C:/ myapp / _jvm / jre / lib / ext -Dlog4j.configuration = file:C:/myapp/realm/conf/log4j.properties -Dorg.apache.commons.logging.Log = or g.apache.commons.logging.impl.Log4JLogger com.myapp.launcher.ProviderLauncher
我已经尝试在类路径周围放置引号,使用双斜线等等无法获得帮助将被赞赏..这里是代码:
String mainClass = args[0]; String propFile = args[1]; FileInputStream fis =new FileInputStream(propFile); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String line = null; ArrayList<String> argList = new ArrayList<String>(); while((line = br.readLine()) != null){ if(line.startsWith("#")) continue; String trimmedLine = line.trim(); if(trimmedLine.isEmpty()) continue; Matcher m = p.matcher(trimmedLine); StringBuffer sb = new StringBuffer(); while(m.find()){ String content = m.group(1); String envContent = System.getenv(content); if(envContent == null || envContent.isEmpty()) envContent = System.getProperty(content); m.appendReplacement(sb, envContent); } m.appendTail(sb); if(!sb.toString().startsWith("\"-") && !sb.toString().startsWith("-") && argList.size() > 0) argList.set(argList.size()-1, argList.get(argList.size()-1)+File.pathSeparator+sb.toString()); else argList.add(sb.toString()); } ArrayList<String> cmdList = new ArrayList<String>(); cmdList.add(System.getenv("JAVA_HOME") + File.separator + "bin" + File.separator + "java"); cmdList.addAll(argList); cmdList.add(mainClass); System.out.println(cmdList.toString()); ProcessBuilder pb = new ProcessBuilder(cmdList); new ProcessBuilder(line); File f = new File(System.getenv("MY_DIR") + File.separator + "logs" +File.separator + "provider-console.log"); if(!f.exists()) f.createNewFile(); pb.redirectErrorStream(true); pb.redirectOutput(f); pb.directory(new File(System.getenv("MY_DIR") + File.separator + "bin")); Process p = pb.start();
这是toStringed cmdList:
[C:/ myapp / _jvm / jre \ bin \ java,-javaagent:C:/myapp/realm/bin/spring-agent.jar,-Xxx256m,-Xms200m,-Xss128k,-XX: + UseBiasedLocking, -XX:+ UseNUMA,-cp C:/myapp/realm/bin/Launcher.jar; C:/ myapp / _jvm / jre /../ lib / tools.jar; C:/ myapp / realm / shared / lib / jetty-util-8.1.4.v20120524.jar,-DprocessType = realm,-Dcert.framework.license = C:/myapp/realm/license/jericho.license,-Djava.security.policy = C:/ myapp / realm /security/java.policy,-Djava.security.properties = C:/myapp/realm/security/java.security,-Djava.security.auth.login.config = C:/myapp/realm/security/jaas.conf ,-Drealm.home = C:/ myapp / realm,-Duser.dir = C:/ myapp / realm / bin,-Djava.endorsed.dirs = C:/ myapp / realm / endorsed; C:/ myapp / _jvm / jre / lib / endorsed,-Djava.ext.dirs = C:/ myapp / realm / ext; C:/ myapp / _jvm / jre / lib / ext,-Dlog4j.configuration = file:C:/ myapp / realm / conf /log4j.properties,-Dorg.apache.commons.logging.Log = org.apache.commons.logging.impl.Log4JLogger,com.myapp.launcher.ProviderLauncher]
答案 0 :(得分:8)
“-cp”和实际的类路径需要是args列表中的单独条目,您当前看起来将它们作为一个以空格分隔的条目。