Jump to content

Relative paths in Java

Go to solution Solved by -iSynthesis,
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^^

Im working on a XMLReader class for a project at school. But I'm having problems with relative paths after compiling code to a .jar file. When compiling with IntelliJ it all works. The XML file i want to read is in the normal Projekt directory so I can open it with 

File file = new File("test.xml");

It also works when using a full path, so C://...//test.xml.

However, after I compile the project to a .jar file it won't read the XML File if it is in the same directory. So, if i have a file, say test.xml, where should i put it in the final project to be able to open it?

My Rig: AMD Ryzen 5800x3D | Scythe Fuma 2 | RX6600XT Red Devil | B550M Steel Legend | Fury Renegade 32GB 3600MTs | 980 Pro Gen4 - RAID0 - Kingston A400 480GB x2 RAID1 - Seagate Barracuda 1TB x2 | Fractal Design Integra M 650W | InWin 103 | Mic. - SM57 | Headphones - Sony MDR-1A | Keyboard - Roccat Vulcan 100 AIMO | Mouse - Steelseries Rival 310 | Monitor - Dell S3422DWG

Link to comment
https://linustechtips.com/topic/784841-relative-paths-in-java/
Share on other sites

Link to post
Share on other sites

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^^

My Rig: AMD Ryzen 5800x3D | Scythe Fuma 2 | RX6600XT Red Devil | B550M Steel Legend | Fury Renegade 32GB 3600MTs | 980 Pro Gen4 - RAID0 - Kingston A400 480GB x2 RAID1 - Seagate Barracuda 1TB x2 | Fractal Design Integra M 650W | InWin 103 | Mic. - SM57 | Headphones - Sony MDR-1A | Keyboard - Roccat Vulcan 100 AIMO | Mouse - Steelseries Rival 310 | Monitor - Dell S3422DWG

Link to comment
https://linustechtips.com/topic/784841-relative-paths-in-java/#findComment-9892174
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×