我想从我的jar中读取资源,如下所示:
File file;
file = new File(getClass().getResource("/file.txt").toURI());
BufferredReader reader = new BufferedReader(new FileReader(file));
//Read the file
Run Code Online (Sandbox Code Playgroud)
并且它在Eclipse中运行时工作正常,但是如果我将它导出到jar中运行它就会出现IllegalArgumentException:
Exception in thread "Thread-2"
java.lang.IllegalArgumentException: URI is not hierarchical
Run Code Online (Sandbox Code Playgroud)
而且我真的不知道为什么,但经过一些测试我发现如果我改变了
file = new File(getClass().getResource("/file.txt").toURI());
Run Code Online (Sandbox Code Playgroud)
至
file = new File(getClass().getResource("/folder/file.txt").toURI());
Run Code Online (Sandbox Code Playgroud)
然后它的工作正好相反(它适用于jar而不是eclipse).
我正在使用Eclipse,我的文件夹在一个类文件夹中.
在Spring Boot 1.1.5和1.1.6中都有这个问题 - 我正在使用@Value注释加载一个类路径资源,当我在STS(3.6.0,Windows)中运行应用程序时,它可以正常工作.但是,当我运行mvn包然后尝试运行jar时,我得到FileNotFound异常.
资源message.txt位于src/main/resources中.我检查了jar并验证它在顶层包含文件"message.txt"(与application.properties相同的级别).
这是应用程序:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application implements CommandLineRunner {
private static final Logger logger = Logger.getLogger(Application.class);
@Value("${message.file}")
private Resource messageResource;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... arg0) throws Exception {
// both of these work when running as Spring boot app from STS, but
// fail after mvn package, and then running as java -jar
testResource(new ClassPathResource("message.txt"));
testResource(this.messageResource);
}
private void testResource(Resource resource) {
try { …Run Code Online (Sandbox Code Playgroud) 我想读取一个位于jar我的类路径中包含的s 之一的XML文件.如何阅读包含在内的任何文件jar?
将 Spring Boot 版本 1.5.7.RELEASE 导出到可运行 JAR 后出现以下错误。出于安全原因,我不使用 Maven,并且我在构建路径中添加了所有 JAR。
我运行下面的命令
java -jar mailer.jar
Run Code Online (Sandbox Code Playgroud)
然后我收到错误,如屏幕截图所示