动态调用 JAVA CLASS/JAR,不是难事。只是随便搜索了一下,没有完整的过程。於是,综合了一下,写不个综合的范例,以便提供参考。 目录结构: view plaincopy to clipboardprint?
{CURDIR}\DynamicLoad.java
\{test}
\testLoad.java
{CURDIR}\DynamicLoad.java
\{test}
\testLoad.java
testLoad.java 的源码: view plaincopy to clipboardprint?
package test;
public class testLoad{
public static String testing(String name){
return "Hello, "+name+"! I am a public static method, I will return a String.";
}
public static void main(String []argv){
System.out.println("------------------------------Begin.");
System.out.println("Hello, dynamac load class testing!");
for(int i=0; i
System.out.println("------------------------------End.");
}
}
package test;
public class testLoad{
public static String testing(String name){
return "Hello, "+name+"! I am a public static method, I will return a String.";
}
public static void main(String []argv){
System.out.println("------------------------------Begin.");
System.out.println("Hello, dynamac load class testing!");
for(int i=0; i
System.out.println("------------------------------End.");
}
}DynamicLoad.java的源码: view plaincopy to clipboardprint?
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
public class DynamicLoad extends URLClassLoader {
public static void main(String[] args) throws Exception {
dyn_load_class();
dyn_load_jar();
}
public static void dyn_load_class() {
File xFile = new File("test"); //指向class所在的目录
URL xUrl;
try {
xUrl = xFile.toURL();
URLClassLoader ClassLoader = new URLClassLoader(new URL[]{xUrl});
Class cd = ClassLoader.loadClass("test.testLoad"); //class名(饱含包名)
Field[] f = cd.getDeclaredFields();
for (int k = 0; k < f.length; k++) {
System.out.println(f[k]);
}
Method mainMethod = cd.getMethod("main", new Class[]{String[].class});
String params[]={"aa","bb"};
mainMethod.invoke(null, new Object[] {params});//执行 main 方法
System.out.println("\n");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void dyn_load_jar() {
DynamicLoad d = new DynamicLoad();
d.addPath("ttest.jar"); //指向 .jar 具体路径
}
public DynamicLoad() {
super(new URL[0], ClassLoader.getSystemClassLoader());
}
public void addPath(String paths) {
if (paths == null || paths.length() <= 0) {
return;
}
String separator = System.getProperty("path.separator");
String[] pathToAdds = paths.split(separator);
for (int i = 0; i < pathToAdds.length; i++) {
if (pathToAdds[i] != null && pathToAdds[i].length() > 0) {
try {
File pathToAdd = new File(pathToAdds[i]).getCanonicalFile();
addURL(pathToAdd.toURL());
System.out.println(pathToAdd.toURL());
Class c = this.loadClass("test.testLoad"); //class名(饱含包名)
Field[] f = c.getDeclaredFields();
for (int k = 0; k < f.length; k++) {
System.out.println(f[k]);
}
Method mainMethod = c.getMethod("testing", new Class[]{String.class});
String params="小橙子";
String ret=(String)mainMethod.invoke(null, params);//执行方法
System.out.println("test.testLoad.testing() return:"+ret);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
[1]
--------------------next---------------------