Java 在 Jar 文件中加载类并获取它的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18594923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:20:31  来源:igfitidea点击:

Load class within Jar file and get it's path

javapluginsjarclassloaderurlclassloader

提问by domizai

I know there are already a lot similar questions here, but I couldn't get any smarter form them. I want to load a class inside a jar file, at this point this is no problem, but when I want to pass the path to my own ClassLoader it throws an exception it cannot find the class. Is it possible to load a class inside a jar using an absolute path? For instance,
Class cls = loader.loadClass(/path/MyPlugin.jar/MyPlugin.class);

我知道这里已经有很多类似的问题,但我无法从它们中得到任何更聪明的答案。我想在 jar 文件中加载一个类,此时这没问题,但是当我想将路径传递给我自己的 ClassLoader 时,它会抛出一个异常,它找不到该类。是否可以使用绝对路径在 jar 中加载类?例如,
Class cls = loader.loadClass(/path/MyPlugin.jar/MyPlugin.class);

But when I do this:

但是当我这样做时:

File test = new File("path/plugins/MyPlugin.jar/MyPlugin.class");
System.out.println(test.exists());

It prints out false. I tried using MyPlugin.jar!/MyPlugin.classor MyPlugin.jar.jar!MyPlugin.classwhich i've seen sometimes on the web, even though i don't really know what it means...

它打印出错误。我尝试使用MyPlugin.jar!/MyPlugin.classMyPlugin.jar.jar!MyPlugin.class有时在网上看到的,即使我真的不知道它意味着什么......

When I do this it finds the class:

当我这样做时,它会找到类:

URLClassLoader loader = new URLClassLoader(new URL[] { "path/plugins/MyPlugin.jar" });
Class cl = loader.loadClass("MyPlugin");

But now, how can I receive the path? Something like URL url = cl.getResource("MyPlugin");(which gives back a null)

但是现在,我怎样才能接收到路径呢?类似的东西URL url = cl.getResource("MyPlugin");(返回一个空值)

采纳答案by Marcin ?o?

You can obtain URLs to classpath resources using ClassLoader.getResources. To find a jar with specific class, you may use the following

您可以使用ClassLoader.getResources获取类路径资源的 URL 。要查找具有特定类的 jar,您可以使用以下命令

URL url = classLoader.getResource("com/example/SomeClass.class");
JarURLConnection connection = (JarURLConnection) url.openConnection();
JarFile file = connection.getJarFile();
String jarPath = file.getName();

where classLoaderis any classloader capable of finding the class you want to load. If the jar is a part of your application's classpath, you may use system classloader:

classLoader任何能够找到您想要加载的类的类加载器在哪里。如果 jar 是应用程序类路径的一部分,则可以使用系统类加载器:

ClassLoader classLoader = ClassLoader.getSystemClassloader();

Otherwise, you need to know the jar file location beforehand, and create an instance of URLClassLoader, passing the jar in the constructor:

否则,您需要事先知道 jar 文件位置,并创建 的实例URLClassLoader,将 jar 传递到构造函数中:

ClassLoader classLoader = new URLClassLoader(new URL[]{new URL("path/to/the/jar/file.jar")});

and then use it to load your class.

然后用它来加载你的类。

回答by schaeferpp

If you want to access a file in your jar file you have to put it in the same directory as the class files are.

如果要访问 jar 文件中的文件,则必须将其放在与类文件相同的目录中。

For example if Main.class is in bin/my/pkg/Main.class you can store your MyPlugin.class in the same directory (bin/my/pkg/MyPlugin.class) and then, if you want to access that file use

例如,如果 Main.class 在 bin/my/pkg/Main.class 中,您可以将 MyPlugin.class 存储在同一目录 (bin/my/pkg/MyPlugin.class) 中,然后,如果您想访问该文件,请使用

URL url = Main.getResource("MyPlugin.class");

Which uses the location of Main.class in your project as root.

它使用项目中 Main.class 的位置作为根。

Hope it helps!

希望能帮助到你!