Solved

java.lang.Compiler?

Posted on 1997-04-18
1
272 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

732 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