简体   繁体   English

从Java运行其他程序

[英]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. 我需要从自己的Java程序中运行其他几个程序,基本上我需要运行这些命令行语句。

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. 我已经尝试使用svn运行Runtime.getRuntime()。exec()了,但是它似乎没有做任何事情,因为它没有生成svn.log文件。 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. 同样,两个程序都需要在不同的地方调用,svn行需要从一个文件夹中调用,而java行则需要从另一个文件夹中调用。

Any ideas on how to go about this? 关于如何解决这个问题的任何想法? If this is not possible in Java, is there a way to do it in C#? 如果在Java中无法做到这一点,那么有没有办法在C#中做到这一点?

Thanks 谢谢

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. 您实际上将要对readLine做一些事情。 I just copied and pasted from code where I didn't care what it said. 我只是从代码中复制并粘贴了我不关心它说什么的地方。

The redirection > (like the pipe | ) is a shell construct and only works when you execute stuff via /bin/sh (or equivalent). 重定向> (例如pipe | )是一个shell构造,仅当您通过/bin/sh (或等效方法)执行内容时才有效。 So the above isn't really going to work. 因此,上面的方法并没有真正起作用。 You could execute 可以执行

/bin/sh -c "svn log --xml -v > svn.log"

and read svn.log . 并阅读svn.log

Alternatively, you can read the output from the process execution and dump that to a file (if you need to dump it to a file, or just consume it directly as you read it). 或者,您可以从流程执行中读取输出并将其转储到文件中(如果您需要将其转储到文件中,或者在读取时直接使用它)。 If you choose this route and consume stdout/stderr separately, note that when you consume the output (stdout), you need to consume stderr as well, and concurrently, otherwise buffers will block (and your spawned process) waiting for your process to consume this. 如果选择此路由并分别使用stdout / stderr,请注意,在使用输出(stdout)时,也需要同时使用stderr,否则,缓冲区将阻塞(和生成的进程),等待进程使用这个。 See this answer for more details. 有关更多详细信息,请参见此答案

instead of piping in your command, just let it print to standard output and error output. 而不是在命令中进行管道传输,只需让它打印到标准输出和错误输出即可。 You can access those streams from your process object that is returned from exec. 您可以从exec返回的流程对象访问这些流。

对于svn东西,请使用java SVNKit API。

Seeing your two commands, why don't you do it directly from Java, without executing ? 看到您的两个命令,为什么不直接从Java直接执行而不执行呢? You could use SVNKit for the svn part, and include directly the jars in your classpath. 您可以将SVNKit用于svn部分,并将jar直接包含在类路径中。

Try this 尝试这个

public static void main(String[] args) {
        try {
            // Execute a command with an argument that contains a space
            System.out.println(args[0]);
            String[]commands = new String[]{"svn", "info", args[0]};
            Process process = Runtime.getRuntime().exec(commands);
            BufferedReader reader =  new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            String result = builder.toString();
            System.out.println(result);

        }catch(Exception e){
            System.out.print(e);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM