ClassPathResource没有获取类路径 [英] ClassPathResource does not get the classpath

查看:3865
本文介绍了ClassPathResource没有获取类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想使用文件夹中存在的资源 media / src / main / resources / testMediaExif

In my application, I would like to use a resource that exist in a folder media/src/main/resources/testMediaExif

为了获得该路径,我使用了这段代码,位于 media / src / main / java / com / project / MyClass.java

To get that path, I used this piece of code, located in media/src/main/java/com/project/MyClass.java:

ClassPathResource resource = new ClassPathResource("classpath:testMediaExif");
File file = resource.getFile();
String absolutePath = file.getAbsolutePath();

显示的错误是:

java.io.FileNotFoundException: class path resource [classpath:testMediaExif] cannot be resolved to URL because it does not exist

如果我更改了该代码:

ClassPathResource resource = new ClassPathResource("testMediaExif");

变量absolutePath取此值:

The variable absolutePath takes this value:

/Users/blanca/desarrollo/media/target/test-classes/testMediaExif

为什么它指向目标路径?我怎么能改变它?

Why does it point to the target path? How could I change it?

推荐答案

我的猜测是绝对路径问题是因为你的maven目标中的outputDirectory POM。在我的项目中,outputDirectory war / WEB-INF / classes,从这里开始执行类。如果我将它更改为某个垃圾值,则不再执行该类。

My guess is that the absolute path issue is because of the outputDirectory in the target of your maven POM . In my project the outputDirectory war/WEB-INF/classes and the it is from here the classes get executed . If I change it to some junk value , the class no longer gets executed .

所以我认为绝对路径必须对.class文件的位置执行某些操作。希望这会有所帮助。

So I believe the absolute path has to do something with the location of your .class files . Hope this helps .

这篇关于ClassPathResource没有获取类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆