Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassPathResource does not get the classpath

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

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();

The error shown is:

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

If I change that code:

ClassPathResource resource = new ClassPathResource("testMediaExif");

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?

like image 782
Blanca Hdez Avatar asked Jan 04 '12 10:01

Blanca Hdez


People also ask

Where is the classpath in spring boot?

It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable . jar , some physical file system location used in the classpath (with java 's -cp option), etc.

How do I add a resource file to a JAR file?

1) click project -> properties -> Build Path -> Source -> Add Folder and select resources folder. 2) create your JAR!

What is the use of classpathresource?

public class ClassPathResource extends AbstractFileResolvingResource Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources. Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.

What is the difference between relativepath and classpathresource?

This implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor. relativePath - the relative path (relative to this resource) This implementation returns the name of the file that this class path resource refers to.

How to load resources from the classpath instead of a file?

Let's instead use resource loading to load resources from the classpath instead of a specific file location. This will work regardless of how the code is packaged: ClassLoader.getResourceAsStream () looks at the classpath for the given resource.

How to solve class path resource cannot be opened problem in Java?

In this post, I would demo how to solve class path resource cannot be opened because it does not exist problem when using java to load file from classpath: 2. Environment 3. The solution As you can see, the file testfiles/test1.txt should be put into the location: ./resources, either the src/main/resources or the src/test/resources directory.


2 Answers

There are two problems with new ClassPathResource("classpath:testMediaExif"):

  1. The classpath: prefix is only used in config files (e.g. XML files), and should not be used if you're using ClasspathResource directly.
  2. classpath:testMediaExif refers to a resource in the root of the classpath, not relative to the file in which you're making the reference.

Try this instead:

new ClasspathResource("testMediaExif", getClass())

or

new ClasspathResource("testMediaExif", MyClass.class)

These will construct a refernce to a resource called testMediaExif relative to MyClass.

One more thing: ClasspathResource.getFile() will only work in the resource really is a file. If it's packed in a JAR, then it won't work.

like image 166
skaffman Avatar answered Oct 13 '22 02:10

skaffman


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 .

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

like image 6
Aravind A Avatar answered Oct 13 '22 04:10

Aravind A