Relative paths in Java
4 hours ago, RUWO_builder said:i don't know if this is neccecary, but its good practice to use a sub dir of your main dir to store xml files.
4 hours ago, Mr_KoKa said:Try to display yourself current working directory so you know where your app is looking for files. Maybe it is because the way you run your app.
I've read a bit more about loading resources in java. The following is my project structure
src/Main.class
resources/test.xml (<- marked as resource folder in IntelliJ)
I tried loading the file as follows:
StringBuilder path = new StringBuilder(""); ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource(resourceName).getFile());
In the main method I printed out the path name it is giving me: "(...)Project\out\production\Code\test.xml".
I'm a bit confused as you might see. I guess the \out\ is where IntelliJ compiles the programm to. The xml file is however shown in a different place in \out\ than the path it earlier gave me. It actually is at "(...)Project\out\production\Code\some\package\test.xml". This is why it gave me a NullPointerException but I'm not sure what I'm doing wrong?
EDIT:
I fixed it, somehow. There were several problems but now it seems to work, thanks anyways^^
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now