简体   繁体   中英

Loading a resource in a JAR file

I have a Java project in Netbeans. It runs fine with Maven. So I assembled it. It contains the following code to load a file that is in the JAR:

ClassLoader loader = MyClass.class.getClassLoader();
SERVICE_URL = loader.getResource("my.wsdl");

This returns a URL like:

jar:file:/path/to/my/file/MyClass-1.0-SNAPSHOT-jar-with-dependencies.jar!/my.wsdl

but the library that needs this parameter doesn't appear to be able to use it.

Is there any way this file can be in the JAR and be referred to from the code like this?

您可能必须使用ClassLoader.getResourceAsStream(),将其复制到一个临时文件,然后使用File.toURI()。toURL()创建一个URL。

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