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
699 views
in Technique[技术] by (71.8m points)

command line - Running other programs from Java

I need to run a couple of other programs from my own Java program, basically I need to run these command line statements.

svn log --xml -v > svn.log

and

java -jar example.jar arg1 arg2

and I need to use the text outputs written to the console from these programs in my own program. I've tried Runtime.getRuntime().exec() with the svn, but it doesn't seem to be doing anything because it doesn't make a svn.log file. Also both programs need to be called in different places, the svn line needs to be called from inside one folder and the java line needs to be called from another.

Any ideas on how to go about this? If this is not possible in Java, is there a way to do it in C#?

Thanks

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Here:

ProcessBuilder processbuilder
try 
{
    processbuilder.directory(file);
    processbuilder.redirectErrorStream(true);

    process = processbuilder.start();

    String readLine;
    BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
    // include this too: 
    // BufferedReader output = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    while((readLine = output.readLine()) != null)
    {
        m_Logger.info(readLine);
    }

    process.waitFor();
}

I've used something similar. You'll actually want to do something with the readLine. I just copied and pasted from code where I didn't care what it said.


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

...