简单Java超编程

21 阅读1分钟

我用的是NetBeans作为开发环境

创建ModulePlugins的Java Class Library,源代码ClassPlugin.java

package programgenesis;

import static java.lang.Math.*;

public class ClassPlugin { public int PluginFunc0(int _x, int _y) { return (int)pow(_x, _y); } }

源代码结束

创建ModuleTest的Java Class Library,源代码ClassTest.java

package programgenesis;

import java.io.File; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader;

public class ClassTest { public int TestFunc0(int _a, int _b, int _c) { try { File _pluginjarfile = new File("D:\NetbeansProject.JAVA\ModulePlugin\dist\ModulePlugin.jar"); URL _jarurl = _pluginjarfile.toURI().toURL(); ClassTest _thisclass = new ClassTest(); URLClassLoader _ucl = new URLClassLoader(new URL[]{_jarurl}, _thisclass.getClass().getClassLoader()); System.out.println("JAR Ready"); if ( _ucl != null ) { Class<?> _lc = _ucl.loadClass("programgenesis.ClassPlugin"); Class[] _argtype = new Class[2]; _argtype[0] = int.class; _argtype[1] = int.class; Method _method = _lc.getDeclaredMethod("PluginFunc0", _argtype); Object _obj = _lc.newInstance(); Object _res = _method.invoke(_obj, _a, _b); Integer _intres = (Integer)_res; if ( _intres.intValue() == _c ) { System.out.println("Test OK!"); return 1; } else { System.out.println("Test Failed"); } }

	} catch (Exception e) {
		
	}
	return 0;
}

} 源代码结束

创建ApplicationEntry的Java Application,源代码ApplicationEntry.java

package programgenesis;

import java.io.File; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader;

public class ApplicationEntry {

public static void main(String[] args) {
	// TODO code application logic here
	try {
		ApplicationEntry _thisclass = new ApplicationEntry();
		
		File _pluginjarfile = new File("D:\\NetbeansProject.JAVA\\ModulePlugin\\dist\\ModulePlugin.jar");
		URL _pluginjarurl = _pluginjarfile.toURI().toURL();
		URLClassLoader _pluginucl = new URLClassLoader(new URL[]{_pluginjarurl}, _thisclass.getClass().getClassLoader());
		System.out.println("ModulePlugin.jar Ready");
		
		File _testjarfile = new File("D:\\NetbeansProject.JAVA\\ModuleTest\\dist\\ModuleTest.jar");
		URL _testjarurl = _testjarfile.toURI().toURL();
		URLClassLoader _testucl = new URLClassLoader(new URL[]{_testjarurl}, _thisclass.getClass().getClassLoader());
		System.out.println("ModuleTest.jar Ready");
		
		if ( _pluginucl != null && _testucl != null ) {
			
			Class<?> _testlc = _testucl.loadClass("programgenesis.ClassTest");
			Class[] _testargtype = new Class[3];
			_testargtype[0] = int.class;
			_testargtype[1] = int.class;
			_testargtype[2] = int.class;
			Method _testmethod = _testlc.getDeclaredMethod("TestFunc0", _testargtype);
			Object _testobj = _testlc.newInstance();
			Object _testres = _testmethod.invoke(_testobj, 2, 3, 8);
			
			Integer _testintres = (Integer)_testres;
			
			if ( _testintres.intValue() == 1 ) {
				System.out.println("Test Module Output : OK");
			} else
			{
				System.out.println("Test Module Output : Failed");
			}
			
			Class<?> _pluginlc = _pluginucl.loadClass("programgenesis.ClassPlugin");
			Class[] _pluginargtype = new Class[2];
			_pluginargtype[0] = int.class;
			_pluginargtype[1] = int.class;
			Method _pluginmethod = _pluginlc.getDeclaredMethod("PluginFunc0", _pluginargtype);
			Object _pluginobj = _pluginlc.newInstance();
			Object _pluginres = _pluginmethod.invoke(_pluginobj, 5, 4);
			
			Integer _intres = (Integer)_pluginres;
			
			System.out.printf("Plugin Module Output : %d\n", _intres.intValue());
			
		}
	
	} catch (Exception e) {
		
	}
}

}

源代码结束

运行ApplicationEntry

输出结果

ModulePlugin.jar Ready

ModuleTest.jar Ready

JAR Ready

Test OK!

Test Module Output : OK

Plugin Module Output : 625

IT人朋友们可修改例子里面的几个数字,观察运行结果变化