ProcessBuilder命令参数

时间:2014-04-24 16:33:27

标签: java linux processbuilder

(对不起我的英语我是法国人)我正在为我的学校项目创建一个小的Java IDE,但是我遇到了在Linux下运行类的问题(我使用的是Debian 7.3),没问题赢8.1

我正在使用ProcessBuilder类来执行带有一些参数的java bin,它们是args和projectOut

args =我们要运行的课程

projectOut =绝对项目路径+“/ out”

package com.esgi.honeycode;

import java.io.*;
import java.util.Scanner;

public class CustomRun {

public static void run(String args, final String projectOut) throws IOException {

    System.out.flush();


    if (args != null && projectOut != null) {

        //SEPARATOR is a const for the file separator
        ProcessBuilder builder = new ProcessBuilder("java", "-classpath", "\"" + System.getProperty("java.class.path") + System.getProperty("path.separator") + projectOut + PropertiesShared.SEPARATOR + "out\"", args);
        System.out.println(builder.command());

        builder.redirectErrorStream(true);
        final Process process = builder.start();


        Thread outThread = new Thread()
        {
            @Override
            public void run() {
                try {

                    String line;
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                        while ((line = reader.readLine()) != null) {
                            System.out.println(line);
                        }
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        };


        Thread inThread = new Thread() {
            @Override
            public void run() {

                Scanner s = new Scanner(System.in);
                //Need to control in before !!

                while (true) {
                    String input = s.nextLine();

                    try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()))) {
                        pw.write(input);
                        pw.flush();
                    }

                }
            }

        };

        outThread.start();
        inThread.start();

    }

}

}

使用简单的类进行测试:

public class MyClass{

public static void main(String[] args)
{
    System.out.println("TESTESTE");
}
}

该类存储在:/ home / m3te0r / HoneyCodeProjects / untitledaaa / out

如果我尝试运行该类,我会使用命令print:

获得此输出

[java,-classpath,“/ home / m3te0r / Bureau / NursCode.jar:/ home / m3te0r / HelpCodeProjects /untitaaa / out”,MyClass]

错误:无法找到或加载主类MyClass

就像我说的,在Win 8.1下没有问题,当我在终端中运行相同的命令时它也有效。

为答案编辑():

好的,所以我弄清楚出了什么问题。 我刚刚删除了类路径中的转义双引号并且它有效。

我当时认为间隔名称或文件存在问题,但没有。

ProcessBuilder builder = new ProcessBuilder("java", "-classpath", System.getProperty("java.class.path") + System.getProperty("path.separator") + projectOut + PropertiesShared.SEPARATOR + "out", args);

0 个答案:

没有答案