簡體   English   中英

在Windows上使用Runtime.getRuntime()。exec()啟動帶有Java進程的空格的類路徑

[英]classpath with spaces for a java process launch with Runtime.getRuntime().exec() on Windows

在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上使用。

您對如何解決此問題有想法嗎?

嘗試逃脫空格:

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

我用簡單的引號將-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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM