Using com.sun.tools.javac.Main() for compiling

J

Jesper Sahner

Hi!

I want to create a dynamic class "on the fly". For this I use:

String[] s = {"Myclass.java"};
(new com.sun.tools.javac.Main()).compile(s);

This works fine. The code reads the .java-file and creates the
..class-file. No problem.

Now I wonder if the same thing can be done without using external
files. The source-code for the .java-file could be held in e.g. a
String-object, and the byte-code as a result of the compilation could
be parsed to a ClassLoader-object.

However I don't how to do this, but I think it must be possible.

Any ideas?

Regards,
Jesper
 
F

Fahd Shariff

You could create a Temporary file which you can delete later on.

Or you can look at javac's source code and see if there is any other
method that might do what you are looking for...
 
H

hiwa

Fahd Shariff said:
You could create a Temporary file which you can delete later on.

Or you can look at javac's source code and see if there is any other
method that might do what you are looking for...

Using temporary file is good because no hack is needed. We can't
access javac's input stream by a standard way.
 
E

ExGuardianReader

hiwa said:
Using temporary file is good because no hack is needed. We can't
access javac's input stream by a standard way.

I've looked at the source code, and there seems to be no other access to
the compiler other than through a file.

This seems very poor design with InputStreams available. We should to be
able to provide an InputStream of Java source code to a compiler method
to generate new bytecode.

Generating skeleton server classes would be much neater if you could do
this.
 
J

Jesper Sahner

How do you access the source code? You only have the byte-code as far as I know.

Regards,
Jesper
 
J

Jesper Sahner

ExGuardianReader said:
I've looked at the source code, and there seems to be no other access to
the compiler other than through a file.

This seems very poor design with InputStreams available. We should to be
able to provide an InputStream of Java source code to a compiler method
to generate new bytecode.

Agreed! There are some byte-code generators around, e.g. BCEL, ASM,
Soot etc., but for this it is "overkill".
Generating skeleton server classes would be much neater if you could do
this.

Could you explain this in more details or add some keywords?

Regards,
Jesper
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top