Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java.lang.Compiler?

Posted on 1997-04-18
1
Medium Priority
?
277 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 600 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

610 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