Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

inputStream.getResoureAsStream results in null in runnable jar file?  RSS feed

 
Lora Twinblade
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings!

I'm trying to get my program to automatically look up the appropriate files in certain folders, something that works when I run the project in Eclipse, but if I export it as a runnable jar file, it can no longer find the directory and ends up giving off a nullpointerexception even though if I specify the file to be loaded manually it seems to work...

It's specifically the inputStream below that ends up as null, I've tried fooling around with different solutions for hours now without finding one... As I said, this somehow works when it's run in Eclipse, but the moment I extract it as a runnable jar file, it can't seem to find it any more.



However, somehow this still works:


Which I wish to avoid however, since I don't necessarily know how many files there will be nor how many folders, and I would like for the program itself to find and load them.

The Sound folder is in the Resources folder, which it can find easily if it's run from Eclipse, however when it's extracted as a runnable jar file the resources folder doesn't seem to be visible and the Sound folder seems to be at the very top, along with the other folders that contain the .class files.

I'm including an image just in case it helps to clarify the file structure, to the left is a screenshot seen from the project opened in Eclipse, to the right is a screenshot seen after opening the runnable jar file with Winrar.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, and it has been some time, your getResourceAsStream is looking in your JAR file, you need to use a File object to get something external to your JAR.

From your image though it looks like you are looking for the resource in the JAR though right? Try getClass().getResourceAsStream.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:If I remember correctly, and it has been some time, your getResourceAsStream is looking in your JAR file, you need to use a File object to get something external to your JAR.

From your image though it looks like you are looking for the resource in the JAR though right? Try getClass().getResourceAsStream.


This "sounds" correct. ;-)

Also, I have had some problems with spaces in file names on some OSes and I usually try to avoid them as much as possible. It shouldn't take long to test just in case...
 
Lora Twinblade
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed the space from the name and added the .getClass(), it still appears to work when running it from Eclipse, but it doesn't seem to solve the problem,

this is what it writes out when run from Eclipse, when it's working:
java.io.ByteArrayInputStream@2a50e9a3

This is what it writes out when run from the runnable jar:
sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@293f78d3
Exception in thread "Thread-2" java.lang.NullPointerException
at java.io.FilterInputStream.read(Unknown Source)
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I read that code, along with the screen shot, it looks to me like you're trying to get an InputStream from a folder. This shouldn't work in any situation at all, so I'm surprised that you say it's working in Eclipse. It looks more like it's reading a file containing a list of file names -- which would actually be more practical. Normally you'd use a File object if you want to get a list of the files in a folder. So I'm confused about what it is which works.

And I understand you plan to put all your classes along with the resources (the .mp3 files and so on) into an executable jar so you can just run that? Unfortunately there isn't really a concept of "folders" in a jar, at least not via the getResource() methods, so you're going to have to work out a different scheme for identifying the resources in the jar. A file containing a list of resource names would be one way to go.
 
Lora Twinblade
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Odd, seems to have the same concept to folders to me? I mean loading all of the files manually still works even in the extracted jar file:



All of the above is working, and is what the code in the first post is meant to load (however it's meant to find them on its own).

 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay... that should work provided the resources are in the right place in the executable jar. But I notice you haven't shown us what's in the jar, we only see what's in your Eclipse project.
 
Lora Twinblade
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I included a screenshot of what it looked like in the first post, but here's a better picture of it:


The class files (such as Content.java) are in the Main/GameState/Entity/Audio/TileMap folders.

@Campbell Ritchie
My apologies, I will try to do better in the future. =/
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find that picture very hard to interpret. It won't even enlarge on my computer (Ubuntu 14.04/Firefox).

Can anybody remember what the best way to copy‑and‑paste a folder structure is? LT: which operating system are you using?
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a little example I whipped up: just use any png or jpg and change the name.

I made this into an executable jar and moved it to another folder and it worked fine. You just have to make sure your path in the getResourceAsStream has the correct path for what you are doing.
 
Lora Twinblade
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A friend of mine managed to find something that works a little bit differently from my attempt, but it's working nonetheless.
I even managed to add a check to see if one is running it via Eclipse or in the jar file. =)



I'll need to work with it a bit more before it can load all of the files properly,
but it seems to find all of the files without any issues.

Thank you all for your help! =)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!