0
有沒有辦法在Java中導入其他類而不將它們添加到類路徑中?像「導入C:/dir/file.jar」?Java import,without CLASSPATH
有沒有辦法在Java中導入其他類而不將它們添加到類路徑中?像「導入C:/dir/file.jar」?Java import,without CLASSPATH
只能通過更改「導入」來導入jar文件。但是你可以使用類加載器。見How to load a jar file at runtime
File file = new File("C:\\dir\\file.jar");
URL url = file.toURL();
ClassLoader classLoader = new URLClassLoader(new URL[]{ file.toURL() });
Class cls = classLoader.loadClass("mypackage.myclass");
[如何裝入在運行時jar文件(http://stackoverflow.com/questions/194698/how-to-load-a-jar-file-at-runtime) – stivlo