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 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...
I hadn't been here for fifteen years
posted 4 years ago
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:
This is what it writes out when run from the runnable jar:
Exception in thread "Thread-2" java.lang.NullPointerException
at java.io.FilterInputStream.read(Unknown Source)
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.
posted 4 years ago
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).
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.
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
posted 4 years ago
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! =)
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you: