Hippopotoman
Hippopotoman

Reputation: 21

Getting the location of a folder next to my .jar file

In a program I am making, I need to access images in a folder placed alongside it. The program works fine when I run it with Eclipse, but when I export it to a .jar file, it does not get the location of the folder properly.

File roomF = new File("Assets/Rooms/1/0.png");

In the IDE, roomF refers to the correct location:

C:\Users\[Username]\Desktop\Eclipse Java\[Project name]\Assets\Rooms\1\0.png

However, in the .jar file, it refers to this:

C:\Users\[Username]\Assets\Rooms\1\0.png.

How can I fix this?

Upvotes: 0

Views: 43

Answers (2)

Rajvir
Rajvir

Reputation: 108

There are two solutions to this:

  1. Place your JAR at:

    'C:\Users[Username]\Desktop\Eclipse Java'

This way, when you are accessing Assets/Rooms/1/0.png, it points to the correct location.

  1. Make a folder where you will place your JAR everytime. Create the path 'Assets/Rooms/1' in the folder and place your file 0.png there, hence you wouldn't be needing to place your JAR each time you make a new one as said in step 1.

Upvotes: 0

Diogo Andrade
Diogo Andrade

Reputation: 58

You should not have resources outside of your JAR or dependencies that need a local path in order to work. Create a resource folder in your project and load the file from there.

Upvotes: 1

Related Questions