2015-04-16 27 views
1

在Java应用程序中,我想执行一个带有多个选项的jar文件。为此,我创建了一个包含所有命令元素的字符串列表,并将其传递给Runtime.exec()方法(这非常简单)。使用Runtime.getRuntime()在Windows上启动Java进程的空间的类路径。在Windows上执行exec()

这里是硬编码字符串的代码(真正的代码使用过程中的变量):

List<String> cmd = new ArrayList<String>(); 
cmd.add("java"); 
cmd.add("-Dpython.path=\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\jython.jar\";\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\Lib\""); 
cmd.add("-cp"); 
cmd.add("\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\..\\build\\jython-engine.jar\";\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\jython.jar\";\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\bin\\..\\plugins\\*\";\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\bin\\..\\kernel\\target\\qtaste-kernel-deploy.jar\";testapi\\target\\qtaste-testapi-deploy.jar"); 
cmd.add("org.python.util.jython"); 
cmd.add("Testbeds\\ControlScripts\\playback.py"); 
cmd.add("start"); 

int exitCode = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]), env, output); 

在Windows 8中,当我这样做的JAVA应用程序中,我得到一个错误:“无法找到或加载主类用“。如果我在控制台中直接执行命令,它就可以工作。我认为这个错误是由于某些路径中的空格造成的,但我不明白如何做更多的用引号括住所有带空格的字符串(就像我已经完成的那样)。

当Linux根目录包含(或不包含)空格时,此代码完美适用于Linux。当根目录不包含空格时,此代码也适用于Windows 8。

你对如何解决这个问题有想法吗?

回答

0

试图逃跑的空间:

List<String> cmd = new ArrayList<String>(); 
cmd.add("java"); 
cmd.add("-Dpython.path=\"C:\\Users\\ange\\Documents\\QTaste\ With\ Spaces\\tools\\jython\\lib\\jython.jar\";\"C:\\Users\\ange\\Documents\\QTaste\ With\ Spaces\\tools\\jython\\lib\\Lib\""); 
cmd.add("-cp"); 
cmd.add("\"C:\\Users\\ange\\Documents\\QTaste\ With\ Spaces\\tools\\jython\\lib\\..\\build\\jython-engine.jar\";\"C:\\Users\\ange\\Documents\\QTaste\ With\ Spaces\\tools\\jython\\lib\\jython.jar\";\"C:\\Users\\ange\\Documents\\QTaste\ With\ Spaces\\bin\\..\\plugins\\*\";\"C:\\Users\\ange\\Documents\\QTaste\ With\ Spaces\\bin\\..\\kernel\\target\\qtaste-kernel-deploy.jar\";testapi\\target\\qtaste-testapi-deploy.jar"); 
cmd.add("org.python.util.jython"); 
cmd.add("Testbeds\\ControlScripts\\playback.py"); 
cmd.add("start"); 

int exitCode = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]), env, output); 

编辑

不要逃避的空间,而是把引号之间的类路径。

对我来说,下面的代码工作,如果我尝试写一个错误的CLASSPATH程序打印在Java

public static void main(String[] args) throws InterruptedException, IOException { 
    List<String> cmd = new ArrayList<String>(); 
    cmd.add("java"); 
    cmd.add("-Dpython.path=\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\jython.jar\";\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\Lib\""); 
    cmd.add("-cp"); 
    cmd.add("\"" 
      + "C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\..\\build\\jython-engine.jar;" 
      + "C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\jython.jar;" 
      + "C:\\Users\\ange\\Documents\\QTaste With Spaces\\bin\\..\\plugins\\*;" 
      + "C:\\Users\\ange\\Documents\\QTaste With Spaces\\bin\\..\\kernel\\target\\qtaste-kernel-deploy.jar;" 
      + "testapi\\target\\qtaste-testapi-deploy.jar;" 
      + "\""); 
    cmd.add("org.python.util.jython"); 
    cmd.add("Testbeds\\ControlScripts\\playback.py"); 
    cmd.add("start"); 

    System.out.println("START..."); 

    Process p = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()])); 

    final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); 
    new Thread(new Runnable(){ 
     public void run() { 
      String line; 
      try { 
       while ((line = in.readLine()) != null) { 
        System.out.println(line); 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     }; 
    }).start(); 

    final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
    new Thread(new Runnable(){ 
     public void run() { 
      String line; 
      try { 
       while ((line = err.readLine()) != null) { 
        System.err.println(line); 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     }; 
    }).start(); 

    int exitStatus = p.waitFor(); 
    System.out.println("exit status: " + exitStatus); 
} 
+0

我试过你的想法,但它不起作用。我确切地说,我已经用“\\”替换了空格以便将它们转义(用“\”替换为不编译)。 – remy40

+0

你说得对,我已经编辑了答案 –

0

给出的错误我通过简单的报价包围-Dpython.path参数和它的作品。 ..

public static void main(String[] args) throws InterruptedException, IOException { 
     List<String> cmd = new ArrayList<String>(); 
//  String qtasteHome = "C:\\Users\\ange\\Documents\\QTaste With Spaces"; //RBA 
//  String qtasteHome = "/home/dev/workspaces/qtaste"; // UBUNTU 
     String qtasteHome = "D:\\Qtaste with spaces"; // Win 8.1 
     cmd.add("java"); 
     cmd.add("-Dpython.path='" + qtasteHome + "\\tools\\jython\\lib\\jython.jar';'" + qtasteHome + "\\tools\\jython\\lib\\Lib'"); 
     cmd.add("-cp"); 
     cmd.add("\"" 
       + qtasteHome + "\\tools\\jython\\lib\\..\\build\\jython-engine.jar;" 
       + qtasteHome + "\\tools\\jython\\lib\\jython.jar;" 
       + qtasteHome + "\\bin\\..\\plugins\\*;" 
       + qtasteHome + "\\bin\\..\\kernel\\target\\qtaste-kernel-deploy.jar;" 
       + "testapi\\target\\qtaste-testapi-deploy.jar;" 
      + "\""); 
     cmd.add("org.python.util.jython"); 
     cmd.add("Testbeds\\ControlScripts\\playback.py"); 
     cmd.add("start"); 

     String command = ""; 
     System.out.println("command parts :"); 
     for (String s : cmd) 
     { 
      System.out.println("\t" + s); 
      command += " " + s; 
     } 

     System.out.println("\nCommand : \n-------\n" + command + "\n-------"); 

     System.out.println("START..."); 

     Process p = Runtime.getRuntime().exec(
       cmd.toArray(new String[cmd.size()])); 

     final BufferedReader in = new BufferedReader(new InputStreamReader(
       p.getInputStream())); 
     new Thread(new Runnable() { 
      public void run() { 
       String line; 
       try { 
        while ((line = in.readLine()) != null) { 
         System.out.println(line); 
        } 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      }; 
     }).start(); 

     final BufferedReader err = new BufferedReader(new InputStreamReader(
       p.getErrorStream())); 
     new Thread(new Runnable() { 
      public void run() { 
       String line; 
       try { 
        while ((line = err.readLine()) != null) { 
         System.err.println(line); 
        } 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      }; 
     }).start(); 

     int exitStatus = p.waitFor(); 
     System.out.println("exit status: " + exitStatus); 
相关问题