How can I access assets directory?

Hello guys,

I use System.getProperty(“user.dir”) + “/assets/” to access some files in the assets directory and it works fine but only if I run the project from jmonkey. After clean and build, this directory is not valid when i run the .jar file.

Is there a simple way to fix this? Do I HAVE to use assetManager? I 've completed my game and this is the final step so I want to avoid major changes in my code.

The assets folder is a jar file in distribution, you cannot change files in there. Make your own folder and register it with the assetManager but note that applications are not allowed to change files in their app folder on modern OSs, you have to create a folder in the user directory.

For getting files from the assets I use this

ImageIO.read( MyClass.class.getClassLoader().getResource("Interface/ico.png");

That was very helpful because some classes don’t have access to the assetManager. Also the files that need to be modified are stored in the user directory as Normen said.

Pesegato’s solution only works if your items are on the classpath… which is very hard to setup in the general case if you want an asset in a modifiable place.

You are better off using the asset manager and simply making sure your classes have access to it. Then you can register whatever paths with it that you want and they need not be part of Java’s classpath.

For classes outside the classPath I use System.getProperty(“user.home”) + “/MyProject/”. Then I create objects from files of this directory using java serializaton. I should probably use assetManager but then I should extend AbstractAppState for that class (or something similar), right?

I don’t see what you’d have to do that.

The only way I know for a class to access the assetManager is through my Main class (which extends SimpleApplication). And all my classes that have a reference to an object of that class extend AbstractAppState

No.
Anything can have anything if you pass it…

public class AnyClass{

      private AssetManager assetManager;
     
      public void AnyClass(AssetManager assetmanager){
           this.assetManager = assetManager;
      }
}

...
//then in your Main class for example
AnyClass ac = new AnyClass(assetManager);
...

And to get back on your issue…You should always use the assetManager. It’s not just an asset loader it also manage caching.
It would have avoided your original issue.
Now what are the files you want to save?
Game saves?
If that’s the case use SaveGame, it will manage to write things where they should in an OS agnostic way.

How stupid of me… of course you can pass anything! It just that in my case is a little more complicated.I use game saves, text files, java files and class files that are loaded dynamically at run time. It’s a game in which you programme a robot.

EDIT: Can i read text files using assetManager? I am a little lost

As said, just register a folder to the assetManager like explained in “Hello Assets”

assetManager.registerLocator(new FileLocator("/path/to/my/folder"));