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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:08:48  来源:igfitidea点击:

specifying a file in a classpathresource

javaspringjarlog4jclasspath

提问by indira

I am using classpathresourceto 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.xmlis the configuration file.

log4j.xml配置文件在哪里。

But its taking a log4j.xmlwhich is in a jar -- sw/merlot/lib/keyManager.jar!/log4j.xml

但它取的log4j.xml是罐子里的一个——sw/merlot/lib/keyManager.jar!/log4j.xml

(Both log4j.xmlare 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.jarcontains my log4j.xmlwhich 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");