简体   繁体   中英

Trouble building executable jar in IntelliJ

I'm trying to export my project to a jar file in IntelliJ using these instructions , and failing.

I'm getting a NullPointerException on this line in my application:

ClassLoader.getSystemClassLoader().getResource(".").getPath(); 

Note that this same project exports fine in Eclipse.

I've tried changing the Class-Path in the manifest to . , as I've noticed this change in the jar exported by Eclipse.

Then, my application gets a bit further, but bails when trying to load any of the resource files included in my jar. This is how:

Assume my jar is placed in

/Users/me/screwed.jar

When my application tries to open resource.file in the root of the jar file, it's searching for it here:

/Users/me/resource.file

This triggers a NullPointerException .

How do I fix this, team?

Use this line to load your path:

this.getClass().getClassLoader().getResource(".").getPath();

This should be the root of your jar file and allow you to open resources within it.

For static context, use

MyStaticClassName.class.getClassLoader().getResource(".").getPath();

where MyStaticClassName is some staic utility class.

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