带有空格的classpath,用于在Windows上使用Runtime.getRuntime()。exec()启动java进程

时间:2015-04-16 14:38:32

标签: java windows runtime.exec spaces

在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应用程序中执行此操作时,出现错误:&#34;无法使用&#34;查找或加载主类。如果我在控制台中直接执行命令,它就可以工作。我认为这个错误是由于某些路径中的空格造成的,但我不知道如何做更多的事情而不是用引号括起所有带空格的字符串(就像我已经完成的那样)。

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

您对如何解决此问题有所了解吗?

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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);