Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 加载依赖于另一个jar文件的jar文件时出现问题_File_Jar_Load_Runtime_Dependencies - Fatal编程技术网

File 加载依赖于另一个jar文件的jar文件时出现问题

File 加载依赖于另一个jar文件的jar文件时出现问题,file,jar,load,runtime,dependencies,File,Jar,Load,Runtime,Dependencies,在运行时加载jar文件时出现问题。 加载My hotel.jar并使用以下代码调用其方法(makeReservation): File file = new File("c:/ComponentFiles/hotel.jar"); URL jarfile = new URL("jar", "", "file:" + file.getAbsolutePath() + "!/"); URLClassLoader cl = URLClassLoader.newInstance(new URL[]{ja

在运行时加载jar文件时出现问题。 加载My hotel.jar并使用以下代码调用其方法(makeReservation):

File file = new File("c:/ComponentFiles/hotel.jar");
URL jarfile = new URL("jar", "", "file:" + file.getAbsolutePath() + "!/");
URLClassLoader cl = URLClassLoader.newInstance(new URL[]{jarfile});
Class  componentClass = cl.loadClass("HotelPackage.HotelMgt");
Object componentObject = componentClass.newInstance();
Method setMethod = componentClass.getDeclaredMethod("makeReservation", null);
setMethod.invoke(componentObject, null);
问题出在jar文件的类HotelPackage.HotelMgt中,我有另一个类(HotelPackage.Hotel)的类变量,它位于另一个jar文件中。 我试图用与上面相同的代码打开并加载另一个jar文件,但收到了找不到类def的异常: 线程“main”java.lang.NoClassDefFoundError中出现异常:BeanPackage/Hotel


解决方案是什么?

您可以通过在JAR的清单文件中定义类路径属性来指定JAR之间的依赖关系。然后JVM将根据需要自动加载依赖项JAR


更多信息在这里:

谢谢,但我找到了另一个真正有效的解决方案。因为我知道将要相互协作的整个组件系列,所以我使用一个类加载器实例(URL数组)加载它们。然后类加载器本身管理依赖项。

在本例中,您可能希望将所有jar放入一个主jar中-因此,您可能对这个项目感兴趣: