开发者

Java import, without CLASSPATH

开发者 https://www.devze.com 2023-04-11 15:08 出处:网络
I开发者_高级运维s 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

I开发者_高级运维s 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");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号