0

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 perfectly. 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. :)

4
  • 1
    Does your executable have any arguments? Can you tell if those arguments are passed correctly to the executable file? Commented Apr 23, 2011 at 16:26
  • +1 for Aleadam's comment. What does your command line look like? Maybe your program simply does not find the input file you mention? Commented Apr 23, 2011 at 16:30
  • Hmm, well the call to the program looks like: program --output=somefilepath. The input file is in a set location. Commented Apr 23, 2011 at 17:10
  • I actually tried putting the call to the external program in a shell script and this runs fine when called from the command line. When I call the script from the Java program it does the same thing: an output file is created but nothing is written to it. Commented Apr 23, 2011 at 17:12

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, I did capture both of those and each are empty. I know the program is running because it creates the xml file as an output. It just isn't writing to it.
Then I would be inclined to say the problem is with your external program or the exact syntax of the command you are executing, not with your Java code as shown.
for small outputs the code may work but the right way is calling proc.waitFor(); after the loops reading the streams. For example try a command like "cmd /C type bigfile" (where bigfile is a text file of 10 MB for example), it only work if proc.waitFor () at the end
0

Did you try launching the process from outside java?

1 Comment

Yeah, it works when called from the command line. I used the same command in my Java code and even used absolute file paths to be sure I wasn't off with those. Since a file is being created by the external program and there is no error message I am fairly sure that my syntax is alright.
0

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.

Comments

0

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!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.