在运行时加载我的 jar 文件时遇到问题。我的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
解决办法是什么?