0

So, I have read all over the internet how to extract my projects to a Runnable Jar File.

I have been able to extract them just fine, but when I try running it, it does nothing.

When I run it in the command prompt with

java -jar ModdingForScrubs.jar

, I get the following output:

https://i.stack.imgur.com/p8C5e.jpg

That specific line that it is pointing to is:

int numOfBackgrounds = new File("resources/background").listFiles().length;

It is producing a Null Pointer Exception. When I run my project in eclipse, it is just fine.

When I export it to a jar, it gets a NPE, which I believe is caused by the fact that it can't find the file.

Here is my Project Workspace:

https://i.stack.imgur.com/JppkO.jpg

Any help would be appreciated.

Visruth
  • 3,430
  • 35
  • 48
user2683809
  • 63
  • 1
  • 5
  • 1
    `new File("resources/background")` is most likely the cause of the exception. The folder cannot be found. You can open the jar file using your favourite zip tool and verify this. As a side note, please post the stack trace as pure text direct in the question, rather than an external screen shot. – Magnilex Sep 21 '14 at 07:08
  • you may use eclipse's option `File > Export > Rannable Jar File`. – Visruth Sep 21 '14 at 07:11

2 Answers2

1

Things to consider.

  1. You should not be using hard coded values, when using File. Once the application is "jarred", the file system location will no longer the the same as when running from your IDE.

  2. If you want to read a jar resource, you should read it a resource via an URL, with getClass().getResource("...") or one of its variants.

Also it looks like (with the path you are using "resources/background"), that the "resource" is on the same level as the src, and is not event being included to the build path of the jar. If you extract the jar, I'm pretty sure the files won't be there.

You should instead put the "resources" inside the "src", so it's automatically put in the build path. You could leave it where it is, and configure the build to add it, but that will take a longer explanation. So just put it in the "src". So you will have a file structure like

ProjectRoot
        src
           resources
                  beackground

Still the problem is trying to read the path a File, which will search for the file using on the file system, using the hard code relative path. That won't work. As I said it should be read from an URL. So you could do something like:

URL url = TestFiles.class.getResource("/resources/background");
URI uri = url.toURI();
File file = new File(uri);
File[] files = file.listFiles();
for (File f : files) {
    System.out.println(f.getAbsolutePath());
}

This should work. So just make sure after you build, that the files actually exist in the jar, then get the url of the dir you want, then you can create a File from the URI

  • See the Class API for more resource obtaining methods, and some explanation.
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks for the help. I switch all my references to getResource(), but and it works great in eclipse, but for some reason when I extract it, it tells me that the url is null. Does it not like getting folders? – user2683809 Sep 21 '14 at 08:55
  • Can't tell without more information to go off of. My suggestion, just create a new project. Add the resources/background package to the src. Add some files in there. Just create one simple class with a `main` and just the code I have above. Create a runnable jar, then run it. It should work. I just tested. If it works for you, then you need to do some debugging to see what you're doing differently in your real application. – Paul Samsotha Sep 21 '14 at 09:03
  • Have a look at [this post](http://stackoverflow.com/a/25636097/2587435) also for some ideas – Paul Samsotha Sep 21 '14 at 10:00
0

It can't find the resources/background folder. That's because the resources folder is out of your src folder, so when packaging the Jar file, the resources folder is not in it - you can open the Jar file and see that.

So I think you should move the resources folder to be in your src folder, and then access to it - now it should be exported with your Jar file.

yishaiz
  • 2,433
  • 4
  • 28
  • 49