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

[526 byte] By [robotcat1980a] at [2007-11-26 20:39:13]
# 1
Read, read again and implement the recommendations in - http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html .
sabre150a at 2007-7-10 1:56:38 > top of Java-index,Java Essentials,Java Programming...
# 2
Crosspost: http://forum.java.sun.com/thread.jspa?threadID=5144765
hunter9000a at 2007-7-10 1:56:38 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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?

kilyasa at 2007-7-10 1:56:38 > top of Java-index,Java Essentials,Java Programming...
# 4
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]
DrLaszloJamfa at 2007-7-10 1:56:38 > top of Java-index,Java Essentials,Java Programming...
# 5

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!

robotcat1980a at 2007-7-10 1:56:38 > top of Java-index,Java Essentials,Java Programming...
# 6
Did you follow that link and read what was there, anyway?I believe com.sun.tools.javac.Main is there in 1.4
DrLaszloJamfa at 2007-7-10 1:56:38 > top of Java-index,Java Essentials,Java Programming...
# 7
> Really urgent to get this done. Please help.... Many thanks in advance! So did you look at the reference I posted in reply #1 ?
sabre150a at 2007-7-10 1:56:38 > top of Java-index,Java Essentials,Java Programming...
# 8

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

kilyasa at 2007-7-10 1:56:38 > top of Java-index,Java Essentials,Java Programming...
# 9

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!

robotcat1980a at 2007-7-10 1:56:39 > top of Java-index,Java Essentials,Java Programming...
# 10

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

sabre150a at 2007-7-10 1:56:39 > top of Java-index,Java Essentials,Java Programming...
# 11
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!
robotcat1980a at 2007-7-10 1:56:39 > top of Java-index,Java Essentials,Java Programming...
# 12
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.
robotcat1980a at 2007-7-10 1:56:39 > top of Java-index,Java Essentials,Java Programming...
# 13
to DrLaszloJamf I will also look at com.sun.tools.javac.Main glad to learn new approach which I did not know before!
robotcat1980a at 2007-7-10 1:56:39 > top of Java-index,Java Essentials,Java Programming...
# 14
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.....
robotcat1980a at 2007-7-10 1:56:39 > top of Java-index,Java Essentials,Java Programming...
# 15
Did you try:import com.sun.tools.javac.Main;
DrLaszloJamfa at 2007-7-21 18:00:05 > top of Java-index,Java Essentials,Java Programming...
# 16
> 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.
DrLaszloJamfa at 2007-7-21 18:00:05 > top of Java-index,Java Essentials,Java Programming...