0

I have a java file named MemoryComparison.java kept in the folder D:\Documents\CodeAnalysis\project_analysis_RG\CodeAnalysis\input\master\Kajari_G

I have another folder D:\Documents\CodeAnalysis\output\ where I want to store the output of the above java file after execution.

In the Java program MemoryComparison.java I have not given any package name.

Now, I am compiling and executing the above java program from another java program named ReadFilesInFolders.java. Below is the code snippet:

try {
    Runtime.getRuntime().exec("javac input\\master\\Kajari_G\\MemoryComparison.java");
    Runtime.getRuntime().exec("java -cp input\\master\\Kajari_G\\ MemoryComparison > output\\output1.txt");
     } catch (IOException e) {
    e.printStackTrace();
     }

Now, the first exec is working fine and the .class file is getting generated in the same folder, where MemoryComparison.java is. But it seems the second exec is not working as no output1.txt is getting created.

But when I am running the above two lines from the command prompt, everything is working fine and the output1.txt is getting created.

Can you please help me with this!

kajarigd
  • 1,299
  • 3
  • 28
  • 46
  • 1
    The space is needed as it is after -cp argument. This exact command is working fine in command line. – kajarigd Feb 20 '14 at 13:07
  • @kajarigd Runtime.exec() executes just what you give it without doing metacharacters and file redirection. Hence, the ">output/output1.txt" makes no sense. If you're going to write batch files or shell scripts in Java, you'll have a hard time **unless** you learn the basics. But then, when you did that, you'll probably just write it in BAT, sh or perl. – Ingo Feb 20 '14 at 13:11

3 Answers3

4

You need to wait for the first process to complete before you execute the next one.

Process p = Runtime.getRuntime().exec("javac ...");
p.waitFor();
p = Runtime.getRuntime().exec("java ...");
p.waitFor();
ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
4

Try this code.

    Process proc = null;
    try {
        String command = "javac input\\master\\Kajari_G\\MemoryComparison.java";
        // Similarly for this: "java -cp input\\master\\Kajari_G\\ MemoryComparison > output\\output1.txt" also           
        proc = Runtime.getRuntime().exec(new String[] { "/bin/sh"//$NON-NLS-1$
                , "-c", command });//$NON-NLS-1$
        if (proc != null) {
            proc.waitFor();
        }
    } catch (Exception e) {
        //Handle
        return;
    }

Edit

On Windows use **cmd** exe path instead of /bin/sh. Check whether -c command line parameter was supported in **cmd** exe.

Idos
  • 15,053
  • 14
  • 60
  • 75
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
0

You need to use ProcessBuilder to redirect... Generally a good practice to use ProcessBuilder API of Java

Here is an example from that starts a process with a modified working directory and environment, and redirects standard output and error to be appended to a log file:

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
File log = new File("log");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
assert pb.redirectInput() == Redirect.PIPE;
assert pb.redirectOutput().file() == log;
assert p.getInputStream().read() == -1;
Ankit Kumar
  • 1,433
  • 1
  • 16
  • 24
  • I am using ProcessBuilder like this: ProcessBuilder builder = new ProcessBuilder("java", "-cp input\\master\\Kajari_G\\ MemoryComparison"); builder.redirectOutput(new File("output\\output1.txt")); builder.redirectError(new File("output\\output1.txt")); Now output file is getting created but with the error: Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. Unrecognized option: -cp input\master\Kajari_G\ MemoryComparison builder.start(); – kajarigd Feb 20 '14 at 13:17
  • You would have added this as comment as it is not a complete answer – Chandrayya G K Feb 20 '14 at 13:22