[英]How to add a class to the ClassPath at runtime?
有一個具有main
方法的jar
文件。 我編譯了一個新類,並且希望將此新類添加到CP。
/folder
/my_jar_file.jar
/MyClass.class
在不重新編譯jar文件的情況下,如何在Java中執行此類操作?
添加到類路徑將不會在運行時加載您的類。 您需要在代碼中顯式加載jar,如下所示:
URLClassLoader classLoader = new URLClassLoader (yourJar.toURL(), this.getClass().getClassLoader());
Class classToLoad = Class.forName ("com.company.abc", true, classLoader);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.