简体   繁体   English

Java Runtime.exec() 程序不会输出到文件

[英]Java Runtime.exec() program won't output to file

I have a program that takes in a file as an input and produces an xml file as an output.我有一个程序,它接受一个文件作为输入并生成一个 xml 文件作为输出。 When I call this from the command line it works perfectly.当我从命令行调用它时,它工作得很好。 I try calling it from a Java program with the following code.我尝试使用以下代码从 Java 程序中调用它。

    try
    {
        Process proc = Runtime.getRuntime().exec(c);

        try
        {
            proc.waitFor();
        }
        catch(InterruptedException e)
        {
            System.out.println("Command failed");
        }
    }
    catch(IOException e)
    {
        System.out.println("Command failed");
        e.printStackTrace();
    }

The program seems to be running fine, as it creates an xml file;该程序似乎运行良好,因为它创建了一个 xml 文件; however, the xml file is empty when I open it.但是,当我打开 xml 文件时它是空的。 I'm not encountering any exceptions in my Java program, so I'm baffled as to what the problem could be.我在我的 Java 程序中没有遇到任何异常,所以我对问题可能是什么感到困惑。 Why would the command line program work fine normally, but then when called from Java not output anything to the file it created.为什么命令行程序可以正常工作,但是从 Java 调用时不会向它创建的文件输出任何内容。 I was thinking maybe it was some sort of permissions thing.我在想也许这是某种权限的事情。 I tried running the program as sudo (I'm using Linux) but to no avail.我尝试以 sudo 运行程序(我使用的是 Linux),但无济于事。 This problem doesn't seem to be anything I could find an answer to online.这个问题似乎不是我能在网上找到答案的任何东西。 Hopefully somebody on here might be able to tell what's going on.希望这里有人能告诉我发生了什么。 :) :)

Get the output and error streams from your process and read them to see what is happening.从您的流程中获取输出和错误流并阅读它们以查看发生了什么。 That should tell you what's wrong with your command.这应该告诉你你的命令有什么问题。

For example:例如:

try {
    final Process proc = Runtime.getRuntime().exec("dir");

    try {
        proc.waitFor();
    } catch (final InterruptedException e) {
        e.printStackTrace();
    }

    final BufferedReader outputReader = new BufferedReader(new InputStreamReader(proc
            .getInputStream()));
    final BufferedReader errorReader = new BufferedReader(new InputStreamReader(proc
            .getErrorStream()));

    String line;

    while ((line = outputReader.readLine()) != null) {
        System.out.println(line);
    }

    while ((line = errorReader.readLine()) != null) {
        System.err.println(line);
    }
} catch (final IOException e) {
    e.printStackTrace();
}

If there is no output in either stream, then I would next examine the external program and the command being sent to execute it.如果任何一个流中都没有输出,那么接下来我将检查外部程序和发送来执行它的命令。

For me, I wrote a jar file that output a file and ran that from the command line in another java program.对我来说,我写了一个 jar 文件来输出一个文件,然后在另一个 java 程序的命令行中运行它。 It turns out that there was a fundamental check in my jar file that I had forgotten about on the number of characters in an input string (my bad).事实证明,我的 jar 文件中有一个基本检查,我忘记了输入字符串中的字符数(我的错)。 If the count of the characters was smaller than 8 there was no output file.如果字符数小于 8,则没有输出文件。 If the number of characters was greater than 8, the output file came out without any trouble using the following code:如果字符数大于 8,则使用以下代码可以毫无问题地输出输出文件:

    String cmdStr = "java -jar somejar.jar /home/username/outputdir 000000001";
    try
    {
        Runtime.getRuntime().exec(cmdStr);
        Runtime.getRuntime().runFinalization();
        Runtime.getRuntime().freeMemory();
        log.info("Done");
    }
    catch (IOException e)
    {
        log.error(System.err);
    }

Not sure if I really need everything here but, hey, it works.不确定我是否真的需要这里的一切,但是,嘿,它有效。 Note: no waitFor seems to be necessary in my case.注意:在我的情况下似乎不需要等待。

您是否尝试从 Java 外部启动该进程?

process input (actually output of the process!) and error streams has to be handled before waiting for the process termination.在等待进程终止之前,必须处理进程输入(实际上是进程的输出!)和错误流。 This should work better这应该会更好

     try 
     {
         Process proc = Runtime.getRuntime().exec("anycomand");

         BufferedReader outSt = new BufferedReader(new InputStreamReader(proc.getInputStream()));
         BufferedReader errSt = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

         String line;

         while ((line = outSt.readLine()) != null) 
         {
             System.out.println(line);
         }

         while ((line = errSt.readLine()) != null) 
         {
             System.err.println(line);
         }

         proc.waitFor();

     } 
     catch (final IOException e) 
     {
         e.printStackTrace();
     }
     

but to understand better how Runtime exec works it is worth reading the classic article但是为了更好地理解 Runtime exec 的工作原理,值得阅读经典文章

When Runtime.exec() won't 当 Runtime.exec() 不会

which provide useful sample code (better than the one above!)它提供了有用的示例代码(比上面的更好!)

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

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