You don't need to mess with class loaders. In fact it's a bad habit to get into because class loader resources are not java.io.File objects when they are in a jar archive.
Maven automatically sets the current working directory before running tests, so you can just use:
File resourcesDirectory = new File("src/test/resources");
resourcesDirectory.getAbsolutePath() will return the correct value if that is what you really need.
I recommend creating a src/test/data directory if you want your tests to access data via the file system. This makes it clear what you're doing.
You don't need to mess with class loaders. In fact it's a bad habit to get into because class loader resources are not java.io.File objects when they are in a jar archive.
Maven automatically sets the current working directory before running tests, so you can just use:
File resourcesDirectory = new File("src/test/resources");
resourcesDirectory.getAbsolutePath() will return the correct value if that is what you really need.
I recommend creating a src/test/data directory if you want your tests to access data via the file system. This makes it clear what you're doing.
Try working with the ClassLoader class:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("somefile").getFile());
System.out.println(file.getAbsolutePath());
A ClassLoader is responsible for loading in classes. Every class has a reference to a ClassLoader. This code returns a File from the resource directory. Calling getAbsolutePath() on it returns its absolute Path.
Javadoc for ClassLoader: http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html
How to get src/main/resources path from Java - Stack Overflow
maven - Java path to file in src/main/resources - Stack Overflow
java - Read file from /src/main/resources/ - Stack Overflow
java - How to get the path to a file in src/main/resources folder? - Stack Overflow
The problem was that I have not included the resource in the maven build in pom file. When I included the following it worked with abcManager.class.getResource("/abc.access")
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>abc.access</include>
</includes>
</resource>
</resources>
</build>
According to the javadoc of URL java.lang.Class.getResource(String name) :
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
- If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise,the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
When you invoke :
abcManager.class.getResource("abc.access")
You don't prefix the path with a "/" character . So, you use the second way.
It means that the resource should be located in
the org.abc.xyz.init package (or folder) but it is not the case since the resource is located in the root of the resources folder.
In Maven, resources is the base directory for resources.
So you can get the resource by invoking the first way:
abcManager.class.getResource("/abc.access")
or you can also simply do it :
getClass().getResource("/abc.access")
Try this.
InputStream is = getClass().getClassLoader()
.getResourceAsStream("/words.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
For best practice, and avoid these problems, put text file (words.txt) to WEB_INF folder (this is secure folder for resources). Then:
ServletContext context = getContext();
InputStream resourceContent = context.getResourceAsStream("/WEB-INF/words.txt");
Reference: https://stackoverflow.com/a/4342095/3728901
You'll be able to elicit the path of the resource in the JAR, but the running program can't know, where your development repository is, unless you supply the information explicitly.
When you run your app, you are not using the resource file in your code folder(src/main/resources) but the ones in the target folder([yourProject]\target as default) where you build to.The target folder path can be changed, it's not a absolute path.
So you can use a "absolute" path out of the content you run or use a "relative" path to the target folder.
Hi there, I have some files in a ressource fioder which I want to access depending on a certain situation. I am trying to build a path to them like: String path = Paths.get("module-name","dir","resources").toString(); Then I build it in: String actualPath = path + separator + fileName;
Even if I put the files in another dir(not in resources) this still not works.
I get file not found exception, system can't find the path. What am I doing wrong?
Thank you!
Do you know how to get a relative path to src/resources from junit test and get all files names in this folder?

