java动态加载_java动态加载

packagetest;importjava.io.File;importjava.io.IOException;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.net.URL;importjava.net.URLClassLoader;importjava.nio.charset.StandardCharsets;importjava.nio.file.Files;importjavax.tools.JavaCompiler;importjavax.tools.ToolProvider;public classSimpleRun {public static voidshowCompile() {//Prepare source somehow.

String source = "package test; public class Test { static { System.out.println(\"hello\"); } public Test() { System.out.println(\"world\");} public void Hello(){System.out.println(\"fuck\");} }";//Save source in .java file.

File root = new File("~/temp"); //On Windows running on C:\, this is//C:\java.

File sourceFile = new File(root, "test/Test.java");

sourceFile.getParentFile().mkdirs();try{

Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8));//Compile source file.

JavaCompiler compiler =ToolProvider.getSystemJavaCompiler();int success = compiler.run(null, null, null, sourceFile.getPath());if (success == 0) {//Load and instantiate compiled class.

URLClassLoader classLoader = URLClassLoader.newInstance(newURL[] { root.toURI().toURL() });

Class>cls;try{//Should print "hello".

cls = Class.forName("test.Test", true, classLoader);//Should print "world".

Object instance =cls.newInstance();//Should print "test.Test@hashcode".

System.out.println(instance);

Method m= cls.getMethod("Hello", new Class>[] {});

System.out.println(m);

m.invoke(instance,newObject[]{});

}catch (ClassNotFoundException | InstantiationException |IllegalAccessException e) {

e.printStackTrace();

}catch(NoSuchMethodException e) {

e.printStackTrace();

}catch(SecurityException e) {

e.printStackTrace();

}catch(IllegalArgumentException e) {

e.printStackTrace();

}catch(InvocationTargetException e) {

e.printStackTrace();

}

}else{

System.out.println("compile failed");

}

}catch(IOException e) {

e.printStackTrace();

}

}private static doublecalculate(String expr) {

String className= "Calculator";

String methodName= "calculate";

String source= "package test; public class " + className + " { public static double " + methodName + "() { return " +expr+ "; } }";//set work directory

File root = new File("~/temp");//set work package

File sourceFile = new File(root, "test/Calculator.java");

sourceFile.getParentFile().mkdirs();try{

Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8));//Compile source file.

JavaCompiler compiler =ToolProvider.getSystemJavaCompiler();int success = compiler.run(null, null, null, sourceFile.getPath());if (success == 0) {//Load and instantiate compiled class.

URLClassLoader classLoader = URLClassLoader.newInstance(newURL[] { root.toURI().toURL() });

Class>cls;try{

cls= Class.forName("test.Calculator", true, classLoader);

Object instance=cls.newInstance();

Method method= cls.getMethod(methodName, new Class>[] {});

Object value= method.invoke(instance, newObject[] {});return(Double) value;

}catch (ClassNotFoundException |IllegalAccessException e) {

e.printStackTrace();

}catch(NoSuchMethodException e) {

e.printStackTrace();

}catch(SecurityException e) {

e.printStackTrace();

}catch(IllegalArgumentException e) {

e.printStackTrace();

}catch(InvocationTargetException e) {

e.printStackTrace();

}catch(InstantiationException e) {

e.printStackTrace();

}

}else{

System.out.println("compile failed");

}

}catch(IOException e) {

e.printStackTrace();

}return 0.0;

}public static voidmain(String[] args) {

SimpleRun.showCompile();if (args.length >= 1) {

System.out.println(args[0] + "=" + SimpleRun.calculate(args[0]));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值