Solved

java.lang.Compiler?

Posted on 1997-04-18
1
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

696 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