1.编译DynamicClassInterface.java2.编译DynamicClass.java将生成的字节码文件DynamicClass.class放入e:\exec3.编译并运行DynamicLoadClassVariable.java确保当前路径中有DynamicClassInterface.class文件,但是没有DynamicClass.class文件DynamicClassInterface.java文件//定义动态装载类的接口publicinterfaceDynamicClassInterface{//返回一个字符串的函数接口方法publicStringgetString();}DynamicClass.java文件publicclassDynamicClassimplementsDynamicClassInterface{publicStringgetString(){//TODOAuto-generatedmethodstubreturnhello;}}DynamicLoadClassVariable.java文件importjava.io.File;importjava.net.URL;importjava.net.URLClassLoader;publicclassDynamicLoadClassVariable{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstub//指定需要被动态装载的类所在路径Filefile=newFile(e:\\exec\\);try{//得到类所在路径的完整的URLURLurl=file.toURI().toURL();URL[]urls=newURL[]{url};//装载类ClassLoadercl=newURLClassLoader(urls);Classcls=cl.loadClass(DynamicClass);//将类变量映射为DynamicClassInterface接口类型DynamicClassInterfaceoe=(DynamicClassInterface)cls.newInstance();//调用DynamicClassInterface接口方法getString,返回动态装载类中定义的结果System.out.println(oe.getString());}catch(Exceptione){System.out.println(e.getMessage());}}}