Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrecognized option: -cp with ProcessBuilder on Windows only

So I am working on a cross platform bootstrap program which works correctly on OSX/Linux but returns the following error message in 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

If I print out my list of ProcessBuilder Arguments and run those arguments on the command line it will run successfully. The command should be

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

I have tried placing quotes around the classpath, using double slashes et cetera to no avail help would be appreciated.. here is the code:

   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();

Here is the 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]

like image 596
user1698672 Avatar asked Sep 25 '12 22:09

user1698672


1 Answers

The "-cp" and the actual classpath need to be separate entries in the args list, you currently appear to have them as one entry separated by a space.

like image 169
Ian Roberts Avatar answered Oct 25 '22 03:10

Ian Roberts