Solved

java.lang.Compiler?

Posted on 1997-04-18
1
268 Views
Last Modified: 2011-09-20
I'm trying to write a program that loads Java source by class name, compiles it and executes it dynamically.  The problem I an having is that the compiler portion of the code doesn't seem to work.  When I attempt to compile using the java.lang.Compiler class, I get a failure to compile.

Has anyone used this class?  Can you provide some source?
0
Comment
Question by:gutz041897
1 Comment
 

Accepted Solution

by:
professorC earned 200 total points
ID: 1219644
As I understand it the class java.lang.Compiler is for JIT
(Just In Time) compilation to native machine code not for
compiling source to bytecodes. If you are using JDK you can
compile using sun.tools.javac.Main. Here is a code fragment
to do that:
        /* error messages go to System.out, the tool is javac*/
      Main compiler = new Main(System.out, "javac");
        /* comp_args is a String[] of args to javac */
      compiler.compile(comp_args); /* compile it */

      /* load the class YourClass  that was just compiled */
      Class temp_class = Class.forName("YourClass");

      /* finally instantiate */
      SomeInterface object = (SomeInterface)
                                temp_class.newInstance();

This only works if java.tools.javac is available. It is
part of JDK but not part of the java core and may not be
in third party systems.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now