Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

how to run a java executable jar in another java program

I know this question has been asked before but those answers didn't provide me an answer.

I want to execute a exec jar file in my java program and get the output from executing jar into a string. Here below are the codes I have used so far without success.

cmdlink = "java -jar iwtest-mac.jar"+" "+cmd;
            System.out.println(cmdlink);
             Process process = Runtime.getRuntime().exec(cmdlink);
             BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
             while ((reader.readLine()) != null) {
                 st = reader.readLine();  

             }
             process.waitFor();

and another code I have tried is as follows:

String cmdlink = "iwtest-mac.jar "+cmd;    
          ProcessBuilder pb = new ProcessBuilder("java", "-jar", cmdlink); //cmd here is a string that contains inline arguments for jar.
            pb.redirectErrorStream(true);
            pb.directory(new File("C:\Users\Dharma"));

            System.out.println("Directory: " + pb.directory().getAbsolutePath());
            Process p = pb.start();
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                    System.out.println( line ); 
            p.waitFor();

Both of the above are not working for me. Any suggestions are appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This works For Me..

public class JarRunner {
    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("java", "-jar", "C:\JCcc.jar");
        pb.directory(new File("C:"));
        try {
            Process p = pb.start();
            LogStreamReader lsr = new LogStreamReader(p.getInputStream());
            Thread thread = new Thread(lsr, "LogStreamReader");
            thread.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class LogStreamReader implements Runnable {

    private BufferedReader reader;

    public LogStreamReader(InputStream is) {
        this.reader = new BufferedReader(new InputStreamReader(is));
    }

    public void run() {
        try {
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This is what the Docs says-

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");

You can pass any number of arguments in constructor.

Read more about process builder here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...