If you want to save your code, you can try changing the working directory in the startup / debugging configuration (the first entry in the list box that provides access to what you want to run) Install this in your root module. But prefer the other approach suggested:
ClassLoader.getSystemResourceAsStream(youPath)
Or my preference:
getClass.getResource(youPath)
or
getClass.getResourceAsStream(youPath)
The leading "/" in the path indicates the working directory of your project, and "/" indicates the relative directory in the current class.
I use this latest solution for my tests: I put my test data resources at the same package level as the test source, or in a subdirectory to avoid a too dirty package.
This way I can make a simple call without a complicated path and not have to deal with the working directory:
project-root - module A - src - test - rootfile.txt - my-complicated-package-naming-root - mypackage - Test.java - testResource.xml
I can get the files this way:
final URL rootfile= Test.class.getResource("/rootfile.txt"); final URL testResource= Test.class.getResource("testResource.xml");
Guillaume Apr 12 2018-11-11T00: 00Z
source share