开发者

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. When I call this from the command line it works perf开发者_如何学编程ectly. I try calling it from a Java program with the following code.

    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; however, the xml file is empty when I open it. I'm not encountering any exceptions in my Java program, so I'm baffled as to what the problem could be. Why would the command line program work fine normally, but then when called from Java not output anything to the file it created. 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. 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.


Did you try launching the process from outside java?


For me, I wrote a jar file that output a file and ran that from the command line in another java program. 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). If the count of the characters was smaller than 8 there was no output file. If the number of characters was greater than 8, the output file came out without any trouble using the following code:

    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.


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

When Runtime.exec() won't

which provide useful sample code (better than the one above!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜