简体   繁体   中英

Exporting Eclipse Project to Runnable Jar

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:

http://imgur.com/FOJKPma

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:

http://imgur.com/034pBW8

Any help would be appreciated.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM