Folders can not be found after converting a JAVA project into a runnable JAR

313
August 20, 2017, at 2:16 PM

I hope somebody can help me with my problem. After hours of scanning all different posts concerning the abovementioned topic I now decided to answer my own question. None of the other posts solved my problem.

I want to export an existing JAVA project as runnable JAR. Therefore I'm changing all absolute references to the following:

this.getClass().getResource("/somekindofimage.png").toString();

This way I can reach all available files in the runnable JAR.

However I need to reference a folder...so I tried it the same way:

this.getClass().getResource("/somekindoffolder").toString();

which gives me the following path:

file:\C:\Users\...\1_data_collection

By creating a file and checking with "is.Directory" it returns false. I unzipped the runnable JAR. All folders, all files that are needed are included. They are attached as sources via BuildPath.

When I test the abovementioned path with Windows Explorer it directs me to the folder I'm looking for.

I'm using Eclipse Neon 2 as IDE, my JAVA Version is 1.8-0_144. Thanks in advance!

Answer 1

You can't refer to files in a JAR using a File path. You have to load them as resources from the classpath.

Make sure the resources and dependencies you need are packaged in the JAR. There's a Maven Shade plugin, if you're using Maven, that will do it for you.

Rent Charter Buses Company
READ ALSO
Java background thread: low priorty

Java background thread: low priorty

I have a web-app which has a background threadThis thread, which is a Executors

374
How can I optimize my 2d java game rendering?

How can I optimize my 2d java game rendering?

My problem is, I have a game with a random generated map and it has only 30-40 fps because of the number of blocks(You can imagine my game like a 2d minecraft)

387
how to know when a select finds 0 rows mysql, getFetchSize() not working

how to know when a select finds 0 rows mysql, getFetchSize() not working

I am trying to make a username only register if that name is not taken, using JDBC connection and checking on SQL DatabaseI have the code that checks for the SELECT * FROM user WHERE username = 'jessica';

307
java getting stack overflow-program is not recursive

java getting stack overflow-program is not recursive

I have been working on my "Random Trivia" program and I keep getting stack overflow with NO recursiveness, at least I cannot find any, here is the code to Guijava, the main class(and only class)

221