java 在类路径资源中指定文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5856116/
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
specifying a file in a classpathresource
提问by indira
I am using classpathresource
to get the resource of a file . In my code am specifying the path so that the file is picked from there . But its taking a file with same name which is already there in a jar from previous.
我classpathresource
用来获取文件的资源。在我的代码中指定路径,以便从那里选择文件。但是它采用了一个同名的文件,该文件已经存在于以前的 jar 中。
i.e., I have specified,
即,我已经指定,
ClassPathResource resource = new ClassPathResource("/sw/merlot/config/log4j.xml")
where log4j.xml
is the configuration file.
log4j.xml
配置文件在哪里。
But its taking a log4j.xml
which is in a jar -- sw/merlot/lib/keyManager.jar!/log4j.xml
但它取的log4j.xml
是罐子里的一个——sw/merlot/lib/keyManager.jar!/log4j.xml
(Both log4j.xml
are different [2 different configuration files])
(两者log4j.xml
都不一样【2个不同的配置文件】)
So i changed it to,
所以我把它改成,
ClassPathResource resource = new ClassPathResource("file:///sw/merlot/config/log4j.xml")
But its still not working .
但它仍然无法正常工作。
I dont know if the way I have specified the file is correct or not?
不知道我指定文件的方式对不对?
I also tried to specify the file using a jar .
我还尝试使用 jar 指定文件。
ClassPathResource resource = new ClassPathResource("jar:file:///sw/merlot/lib/keyManager111.jar!/log4j.xml")
where keyManager111.jar
contains my log4j.xml
which i want. But its still not picking from the jar. Its telling file not found at the specified location.
哪里keyManager111.jar
包含我log4j.xml
想要的。但它仍然没有从罐子里采摘。在指定位置找不到它的通知文件。
回答by jabal
What about:
关于什么:
ClassPathResource resource = new ClassPathResource("classpath:/sw/merlot/config/log4j.xml");
or if it is in a different jar file:
或者如果它在不同的 jar 文件中:
ClassPathResource resource = new ClassPathResource("classpath*:/sw/merlot/config/log4j.xml");