简体   繁体   中英

Java import, without CLASSPATH

Is there a way to import other classes in Java without adding them to the classpath? Something like "import C:/dir/file.jar"?

You can't import a jar file just by changing the "import". But you can using the class loader. See 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM