Solved

java.lang.Compiler?

Posted on 1997-04-18
1
263 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Performance issue while iterating with streams 5 39
array11 challenge 16 52
countAbc challenge 9 49
Problem to start Neon 20 54
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

760 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

20 Experts available now in Live!

Get 1:1 Help Now