2

In the following example, I'm trying to use sun.tools.javac.Main to dynamically compile a class I generate, then instantiate an object of that class and invoke a method. So far, I can't even get passed loading the generated class. I get the following exception in Eclipse:

java.lang.ClassNotFoundException: TestHello_1289950330167
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at MyClassGenerator.runIt(MyClassGenerator.java:47)
    at MyClassGenerator.main(MyClassGenerator.java:13)
Note: sun.tools.javac.Main has been deprecated.
1 warning
Running TestHello_1289950330167:

Here's the code:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.net.URLClassLoader;

public class MyClassGenerator {
    String generatedClassName = "TestHello_" + System.currentTimeMillis();
    String javaFileName = this.generatedClassName + ".java";

    public static void main(final String args[]) {
        final MyClassGenerator mtc = new MyClassGenerator();
        mtc.createIt();
        if (mtc.compileIt()) {
            System.out.println("Running " + mtc.generatedClassName + ":\n\n");
            mtc.runIt();
        }
        else {
            System.out.println(mtc.javaFileName + " is bad.");
        }
    }

    public void loadIt() {

        final ClassLoader classLoader = MyClassGenerator.class.getClassLoader();

        try {
            final Class aClass = classLoader.loadClass(this.generatedClassName);
            System.out.println("Loaded " + aClass.getName());
        }
        catch (final ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public void createIt() {
        try {
            final FileWriter aWriter = new FileWriter(this.javaFileName, true);
            aWriter.write("public class " + this.generatedClassName + " { }");
            aWriter.flush();
            aWriter.close();
        }
        catch (final Exception e) {
            e.printStackTrace();
        }
    }

    public boolean compileIt() {
        final String[] source = { new String(this.javaFileName) };
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        new sun.tools.javac.Main(baos, source[0]).compile(source);

        System.out.print(baos.toString());

        return (baos.toString().indexOf("error") == -1);
    }

    public void runIt() {
        try {
            final File file = new File(this.javaFileName);
            final URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { file.toURI().toURL() });
            final Class<?> cls = Class.forName(this.generatedClassName, true, classLoader);
        }
        catch (final Exception e) {
            e.printStackTrace();
        }
    }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124

2 Answers2

6

Because it's not in the classpath. Either write it to the classpath (or add its root path to the classpath) or use URLClassLoader.

File root = new File(".");
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
Class<?> cls = Class.forName(generatedClassName, true, classLoader);

Using relative paths in java.io is by the way a bad idea. You're dependent on the current working directory which is not controllable from in the code.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I still get the error. I've updated the code with your suggestion. I'd really like to be able to do this dynamically. – Amir Afghani Nov 16 '10 at 23:45
  • My mistake, you have to set the root path as classpath, not the file itself. I fixed the code example. I would however recommend to use a fixed root path anyway. Being dependent on current working directory is a bad idea. You can by the way find another example in [this answer](http://stackoverflow.com/questions/1981750/loading-external-source-code-and-using-them-internally-by-re-compiling-or-someth/1981919#1981919). – BalusC Nov 16 '10 at 23:49
  • If I put my generated class in a package, would that package name be used instead of '.'? – Amir Afghani Nov 16 '10 at 23:50
  • 1
    No, it must then be included in `generatedClassName`. It should actually represent the FQN (Full Qualified Name) like `com.example.ClassName`. – BalusC Nov 16 '10 at 23:51
1

You are creating a new URLClassLoader which points to a concrete file. Expressed as command line arguments, you're doing it like this:

java -cp file:///foo/bar/TestHello_1289950330167.java

And then your code calls this:

Class.forName("TestHello_1289950330167",true,cl);

A class path is either a JAR file or a folder, not a .java file!

What you should do is creating an URLClassLoader with ".".toURI().toURL() as the class path, not the javaFileName.

mhaller
  • 14,122
  • 1
  • 42
  • 61