2012-09-25 38 views
2

所以我工作的一個跨平臺的引導程序,它正確地工作在OSX/Linux,但返回在Windows以下錯誤信息:無法識別的選項:帶的ProcessBuilder -cP僅在Windows

Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. Unrecognized option: -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, -Xmx256m, -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

你能分享更多關於原始錯誤信息嗎?你什麼時候看到它?有沒有相關的堆棧跟蹤? – allingeek

回答

6

的「-cp」和實際的類路徑必須在的args列表單獨條目,您目前似乎有他們作爲用空格隔開一個條目。

+0

感謝第二組眼睛,解決了問題。你知道爲什麼OS不同嗎? – user1698672

+0

不知道,對不起,我本來會期望所有平臺上出現同樣的錯誤... –

+0

誰知道!解決了我的問題。 – marcolopes