【问题标题】:classpath with spaces for a java process launch with Runtime.getRuntime().exec() on Windows带有空格的类路径,用于在 Windows 上使用 Runtime.getRuntime().exec() 启动 java 进程
【发布时间】:2015-06-23 01:21:44
【问题描述】:

在 Java 应用程序中,我想执行一个带有多个选项的 jar 文件。为此,我构建了一个包含所有命令元素的字符串列表,并将其传递给Runtime.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 上完美运行。当根目录不包含空格时,此代码也适用于 Windows 8。

您知道如何解决这个问题吗?

【问题讨论】:

    标签: java windows runtime.exec spaces


    【解决方案1】:

    我用简单的引号包围了 -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);
    

    【讨论】:

      【解决方案2】:

      尝试转义空格:

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

      【讨论】:

      • 我试过你的想法,但它不起作用。我确切地说我已经用“\\”替换了空格以逃避它们(用“\”替换不会编译)。
      猜你喜欢
      • 2011-04-15
      • 2016-01-09
      • 2013-07-07
      • 1970-01-01
      • 2013-11-10
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      相关资源
      最近更新 更多