从另一个jar文件访问资源 [英] Access resources from another jar file
问题描述
我有一个简单的结构:一个包含大量数据的数据jar文件,以及一个使用数据运行服务的服务jar文件.为了使数据易于替换,我将它们分开,并且service.jar的类路径包含data.jar所在的目录.
I have a simple structure: A data jar file which contains a batch of data, and a service jar file, which runs a service using the data. To make the data easy to replace, I have them separate, and service.jar's classpath contains the directory which data.jar is in.
在service.jar中,我使用getResource加载数据文件.如果数据文件直接位于文件夹中,则有效,但是当它们位于data.jar中时会失败;
Within service.jar, I use getResource to load the data files. This works if the data files are directly within the folder, but fails when they are inside data.jar;
此操作失败:
all
+ globalclasspath
| + data.jar
| + mine.properties
+ daemons
+ service.jar
jsvc -cp globalclasspath:daemons/service.jar (...)
MyClass.class.getClassLoader( ).getResource( "mine.properties" ); // <-- null
但这可行:
all
+ globalclasspath
| + mine.properties
+ daemons
+ service.jar
jsvc -cp globalclasspath:daemons/service.jar (...)
MyClass.class.getClassLoader( ).getResource( "mine.properties" ); // <-- not null
我不想更改类路径(除非我可以将其更改为不依赖于数据jar文件名称的通用名称),但是我可以更改getResource字符串(我已经尝试了/data/mine.properties和/data.jar/mine.properties都没有用).我可以做些更改,以便可以从jar中加载资源吗?
I don't want to change the classpath (unless I can change it to something generic which doesn't depend on the name of the data jar file), but I'm fine with changing the getResource string (I've tried /data/mine.properties and /data.jar/mine.properties to no avail). Is there a change I can make so that the resources can be loaded from within the jar?
推荐答案
解决方案1
使用类路径通配符.
jsvc -cp globalclasspath/*:daemons/service.jar (...)
解决方案2
To read data in JARs not on the classpath, use URLClassLoader
. The general algorithm is this:
- 在
globalclasspath
目录中找到JAR列表. - 从此JAR列表中创建
URLClassLoader
. - 从
URLClassLoader
实例中查找所需的资源.
- Find the list of JARs in the
globalclasspath
directory. - Create a
URLClassLoader
from this list of JARs. - Look up the resource you want from the
URLClassLoader
instance.
要在类路径上找到JAR,我使用了StackOverflow文章"
To find JARs on the classpath, I used ResourceList
from the StackOverflow article "Get a list of resources from classpath directory."
public class MyClass {
/**
* Creates a {@code URLClassLoader} from JAR files found in the
* globalclasspath directory, assuming that globalclasspath is in
* {@code System.getProperty("java.class.path")}.
*/
private static URLClassLoader createURLClassLoader() {
Collection<String> resources = ResourceList.getResources(Pattern.compile(".*\\.jar"));
Collection<URL> urls = new ArrayList<URL>();
for (String resource : resources) {
File file = new File(resource);
// Ensure that the JAR exists
// and is in the globalclasspath directory.
if (file.isFile() && "globalclasspath".equals(file.getParentFile().getName())) {
try {
urls.add(file.toURI().toURL());
} catch (MalformedURLException e) {
// This should never happen.
e.printStackTrace();
}
}
}
return new URLClassLoader(urls.toArray(new URL[urls.size()]));
}
public static void main(String[] args) {
URLClassLoader classLoader = createURLClassLoader();
System.out.println(classLoader.getResource("mine.properties"));
}
}
我运行了以下命令:
java -cp globalclasspath:daemons/service.jar MyClass
终端输出:
jar:file:/workspace/all/globalclasspath/data.jar!/mine.properties
这篇关于从另一个jar文件访问资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!