2015-04-16 29 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); 
相關問題