tags:

views:

200

answers:

6
    import java.lang.*;
import java.io.*;

class test 
{
    public static void main(String[] a) throws Exception
    {
        int i;
        String[] str = new String[]{"javac","example.java"};
        String[] str1 = new String[]{"java","example"};
        Runtime r = Runtime.getRuntime();
        Process p = null;
        Process p1 = null;
        p=r.exec(str);
        p1=r.exec(str1);
        InputStreamReader reader = new InputStreamReader (p1.getInputStream ());
        BufferedReader br = new BufferedReader(reader);
        FileWriter fw = new FileWriter("this.txt",true);
        char[] c = new char[1];
        while ((i=br.read())!=-1)
        {
            c[0]     = (char) i ;
            fw.write(c);
            c = new char[1];
        }
        fw.close();
    }
}

this a simple program using runtime class. is there any termination of 'process' code need to be employed?

Thanks in advance!!!

+4  A: 

1 . There's no reason to initialize the process objects to null.

Just do:

Process p = r.exec(str);
Process p1 = r.exec(str1);

While you're at it, better variable names would help

2 . You can improve performance by reading and writing more than 1 character at a time:

3 . You may want to explicitly specify encodings, rather than using the platform default.

InputStreamReader reader = new InputStreamReader (p1.getInputStream (), inputCharsetName);
BufferedReader br = new BufferedReader(reader);
FileOutputStream fos = new FileOutputStream("this.txt", true);
Writer writer = new OutputStreamWriter(fos, outputCharsetName);
Matthew Flaschen
thank u !!! and i also included a finally block!!
billu
+3  A: 

The input stream is never closed, and I recommend closing the writer in a finally block.

Adam Crume
While it is "best practice" to do that, in this particular case it will make no difference ... except if the "main" entry point is called from another application in the same JVM.
Stephen C
@Stephen C: It's still best to get into the habit of putting your clean-up code into a `finally` block, even where it is not strictly speaking necessary. It rarely hurts to put in the `finally` block even in `main`, but it does hurt if you forget to do it elsewhere.
JUST MY correct OPINION
Actually, it is best to understand **why** you are doing this, when it is unnecessary, then make a *rational* decision to do it or not do it. And by the way, an unnecessary `finally` block **can** be harmful, if (for example) it is the vehicle for introducing an NPE bug.
Stephen C
A: 

is there any termination of 'process' code need to be employed?

I think that depends on the processes - will they terminate on their own? (And do you want them to terminate when this program does?)

You can terminate easily with:

p.destroy();
p1.destroy();
Greg Harman
A: 

Your program will not work the way as you would expect it to. ie. it will try to run the example.class before even the compilation of example.java is completed.

chedine
it works and a file is created!!
billu
i´d say it works because the file is already present, try to delete the class file and run it again. If it still works: good for you but bad technique, if the class will take some time to compile it will fail.
atamanroman
File will be created.No problem with your first process.There is no gurantee that p1 will run only after your process 'p' has completed.
chedine
+1  A: 

You should use Process.waitFor() in order to let the compiling process complete. Otherwise you will likely try to run a non-existing class file because compilation took too long.

The buffer is too small, everytime you do something like char[] c = new char[1]; // lolz array with exactly one element you should look at least twice.

atamanroman
+1 for the first sentence
sje397
A: 

First, you need to wait for the compilation to complete before executing the second process. Use the waitFor() method:

int compilationResult = -1;
try {
  compilationresult = p.waitFor();
}
catch (InterruptedException ie) {
  ...
}

Then check compilationResult (a value of 0 indicates successful compilation) to determine whether to continue with the rest of the code.

Secondly, if you're finished with the p1 process after processing its output, then you should either wait for it to finish, or kill it yourself - depending on whether you expect the process to complete by itself or not.

To wait for the process to complete normally, use a similar block of code to above, by enclosing p1.waitFor() in a try/catch block.

To kill it yourself, use:

p1.destroy();

After you're sure the process has completed, you might then want to check the exit value with p1.exitValue(). Why be so careful about this? Well, you probably shouldn't trust the output you're collecting and writing to this.txt until you're sure the process executed correctly.

Ryan Bennetts