user647569
user647569

Reputation:

Netbeans: Accessing resources in other packages

I have switched over to Netbeans and am finding accessing files in packages quite perplexing. Usually I would just have everything in the one folder.

I have created packages in my project which is structured like so:

project_name  
......Source Packages  
............Game  
............Players  
............Resources  
..................Levels  
..................Images  

I want to access a text file that resides in the Resources.Levels package from my Gui class which resides in my Game package. How would I do this? Ideally I want to create a String = "mapFile.txt"

Thanks

Upvotes: 1

Views: 1578

Answers (2)

JB Nizet
JB Nizet

Reputation: 692171

This has nothing to do with NetBeans, but everything to do with Java. To access a resource in the classpath, you should use Class.getResourceAsStream() method. Read its documentatino carefully. In your case, you should use

Gui.class.getResourceAsStream("/Resources/Levels/mapFile.txt");

Also, packages in Java should be in all lowercase.

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240966

MyClass.class.getResources("/Resources/Image/myImage.jpg");

Upvotes: 3

Related Questions