how to get compiler message after run Runtime.getRuntime.exec("javac..")
Hi all. I am writing a ClassCompiler class which contains command to compile other class. I write the following:
Process p = Runtime.getRuntime().exec("javac test.java");
InputStream in = p.getInputStream();
int c;
while ((c = in.read()) != -1) {
System.out.print((char)c);
}
in.close();
after running, the there is nothing to display on the Eclipse console. I want to see the message of compilation (I made some type incompatibility in test.java for testing reason).
Read, read again and implement the recommendations in - http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html .
Crosspost: http://forum.java.sun.com/thread.jspa?threadID=5144765
> Hi all. I am writing a ClassCompiler class which
> contains command to compile other class. I write the
> following:
> Process p = Runtime.getRuntime().exec("javac
> test.java");
> InputStream in = p.getInputStream();
> int c;
> while ((c = in.read()) != -1) {
> System.out.print((char)c);
>
> in.close();
>
> after running, the there is nothing to display on the
> Eclipse console. I want to see the message of
> compilation (I made some type incompatibility in
> test.java for testing reason).
just out of curiosity why are you trying to compile it through java code. Have you tried antor something like that?
If you are using the current version (Java SE 6), the compiler isexposed by the API of javax.tools.JavaCompiler.You don't have to use that Runtime.exec hack.[url= http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html#proginterface]JavaCompiler[/url]
Hi, I am embedded this in my code for implementing a tool to check the type compatibility of communicating java classes. Therefore I wanna run javac and export the error message when the type dismatch detected by the compiler.
Currently when I run this Runtime.getRuntime.exec("javac test.java"), I check the test.class generated time and it is generated by running this command. However I cannot get out the compiler message!
Another interesting thing is I tried Runtime.getRuntime.exec("ls") with the following code and it displays all the sub directories!! It just does not work for javac so far..bizarre!
I am now using JDK1.4.2...dont think will get upgraded to SE6....sorry
Really urgent to get this done. Please help.... Many thanks in advance!
Did you follow that link and read what was there, anyway?I believe com.sun.tools.javac.Main is there in 1.4
> Really urgent to get this done. Please help.... Many thanks in advance! So did you look at the reference I posted in reply #1 ?
I would strongly recommend using some kind of a build file or a build script for the purpose rather than using java. Runtime has not been provided for the purpose. Doing it right the first time would save you a lot of trouble in future if you have to support it as well down the road. Unless you are a contractor and dont care about the misery of the person who is going to support this tool
A large thankyou to you guys!! the problems are now solved!
just one silly mistake:
I wrote "InputStream in = p.getInputStream();"
This should not be InputStream..should be ErrorStream
change to "InputStream in = p.getIErrorStream();"
then the compilation errors can now be displayed!!
Thank you soooo much for giving me hints!
> A large thankyou to you guys!! the problems are now
> solved!
> just one silly mistake:
> I wrote "InputStream in = p.getInputStream();"
> This should not be InputStream..should be
> ErrorStream
> change to "InputStream in = p.getIErrorStream();"
> then the compilation errors can now be displayed!!
>
> Thank you soooo much for giving me hints!
If you had read the reference I posted in reply #1 you would have found this. I think I wasted my time!
to sabre150: nonono...u did me a huge favour! I did read the link you sent me and from which I realised the silly mistake I made!!Thank you very much!
to kilyas:your approach sounds interesting. However I dont know about build file and build script. Can you give me some examples for this?Basically I am seeking for some way to do type checking.
to DrLaszloJamf I will also look at com.sun.tools.javac.Main glad to learn new approach which I did not know before!
Hi, I also tried com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main()but when I import com.sun.tools.* the compiler says this cannot be resolved.I am using JDK1.4.2.....
Did you try:import com.sun.tools.javac.Main;
> Hi, I also tried com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main()Main probably doesn't have a public constructor. Note that the compile methods are static.