Compiling Java code from string results in ClassNotFoundException -
i'm trying code example uses javax.tools compile code in string. class.forname results in classnotfoundexception. know why? i'm using java 7.
import java.io.ioexception; import java.io.printwriter; import java.io.stringwriter; import java.lang.reflect.invocationtargetexception; import java.net.uri; import java.util.arraylist; import java.util.arrays; import java.util.list; import javax.tools.diagnostic; import javax.tools.diagnosticcollector; import javax.tools.javacompiler; import javax.tools.javafileobject; import javax.tools.simplejavafileobject; import javax.tools.toolprovider; import javax.tools.javacompiler.compilationtask; public class main { public static void main(string args[]) throws ioexception { javacompiler compiler = toolprovider.getsystemjavacompiler(); diagnosticcollector<javafileobject> diagnostics = new diagnosticcollector<javafileobject>(); stringwriter writer = new stringwriter(); printwriter out = new printwriter(writer); out.println("public class helloworld {"); out.println(" public static void main(string args[]) {"); out.println(" system.out.println(\"this in java file\");"); out.println(" }"); out.println("}"); out.close(); javafileobject file = new javasourcefromstring("helloworld", writer.tostring()); iterable<? extends javafileobject> compilationunits = arrays.aslist(file); compilationtask task = compiler.gettask(null, null, diagnostics, optionlist, null, compilationunits); boolean success = task.call(); (diagnostic diagnostic : diagnostics.getdiagnostics()) { system.out.println(diagnostic.getcode()); system.out.println(diagnostic.getkind()); system.out.println(diagnostic.getposition()); system.out.println(diagnostic.getstartposition()); system.out.println(diagnostic.getendposition()); system.out.println(diagnostic.getsource()); system.out.println(diagnostic.getmessage(null)); } system.out.println("success: " + success); if (success) { try { class.forname("helloworld").getdeclaredmethod("main", new class[] { string[].class }) .invoke(null, new object[] { null }); } catch (classnotfoundexception e) { system.err.println("class not found: " + e); } catch (nosuchmethodexception e) { system.err.println("no such method: " + e); } catch (illegalaccessexception e) { system.err.println("illegal access: " + e); } catch (invocationtargetexception e) { system.err.println("invocation target: " + e); } } } } class javasourcefromstring extends simplejavafileobject { final string code; javasourcefromstring(string name, string code) { super(uri.create("string:///" + name.replace('.','/') + kind.source.extension), kind.source); this.code = code; } @override public charsequence getcharcontent(boolean ignoreencodingerrors) { return code; } }
the classloader has no idea of helloworld
class. can this:
urlclassloader classloader = urlclassloader.newinstance(new url[] { root.touri().tourl() }); //root path class file class<?> cls = class.forname("helloworld", true, classloader);
you can see a full example here.
Comments
Post a Comment