Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open resource with relative path in Java

In my Java app I need to get some files and directories.

This is the program structure:

./main.java ./package1/guiclass.java ./package1/resources/resourcesloader.java ./package1/resources/repository/modules/   -> this is the dir I need to get ./package1/resources/repository/SSL-Key/cert.jks    -> this is the file I need to get 

guiclass loads the resourcesloader class which will load my resources (directory and file).

As to the file, I tried

resourcesloader.class.getClass().getResource("repository/SSL-Key/cert.jks").toString() 

in order to get the real path, but this way does not work.

I have no idea which path to use for the directory.

like image 919
Giancarlo Avatar asked Feb 21 '09 20:02

Giancarlo


People also ask

How do you specify a resource path in Java?

The simplest approach uses an instance of the java. io. File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file.

How do I view the resources folder in a jar file?

This works when running inside and outside of a Jar file. PathMatchingResourcePatternResolver r = new PathMatchingResourcePatternResolver(); Resource[] resources = r. getResources("/myfolder/*"); Then you can access the data using getInputStream and the filename from getFilename .

How do you specify a file path in FileInputStream?

This String should contain the path in the file system to where the file to read is located. Here is a code example: String path = "C:\\user\\data\\thefile. txt"; FileInputStream fileInputStream = new FileInputStream(path);

How do you load properties file from resources folder in Java?

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass(). getClassLoader().


1 Answers

I had problems with using the getClass().getResource("filename.txt") method. Upon reading the Java docs instructions, if your resource is not in the same package as the class you are trying to access the resource from, then you have to give it relative path starting with '/'. The recommended strategy is to put your resource files under a "resources" folder in the root directory. So for example if you have the structure:

src/main/com/mycompany/myapp 

then you can add a resources folder as recommended by maven in:

src/main/resources 

furthermore you can add subfolders in the resources folder

src/main/resources/textfiles 

and say that your file is called myfile.txt so you have

src/main/resources/textfiles/myfile.txt 

Now here is where the stupid path problem comes in. Say you have a class in your com.mycompany.myapp package, and you want to access the myfile.txt file from your resource folder. Some say you need to give the:

"/main/resources/textfiles/myfile.txt" path 

or

"/resources/textfiles/myfile.txt" 

both of these are wrong. After I ran mvn clean compile, the files and folders are copied in the:

myapp/target/classes  

folder. But the resources folder is not there, just the folders in the resources folder. So you have:

myapp/target/classes/textfiles/myfile.txt  myapp/target/classes/com/mycompany/myapp/* 

so the correct path to give to the getClass().getResource("") method is:

"/textfiles/myfile.txt" 

here it is:

getClass().getResource("/textfiles/myfile.txt") 

This will no longer return null, but will return your class. I hope this helps somebody. It is strange to me, that the "resources" folder is not copied as well, but only the subfolders and files directly in the "resources" folder. It would seem logical to me that the "resources" folder would also be found under "myapp/target/classes"

like image 96
Johnny Avatar answered Oct 06 '22 02:10

Johnny