java - Exporting Eclipse Project to Runnable Jar -
so, have read on internet how extract projects runnable jar file.
i have been able extract them fine, when try running it, nothing.
when run in command prompt
java -jar moddingforscrubs.jar
, following output:
that specific line pointing is:
int numofbackgrounds = new file("resources/background").listfiles().length;
it producing null pointer exception. when run project in eclipse, fine.
when export jar, gets npe, believe caused fact can't find file.
here project workspace:
any appreciated.
things consider.
you should not using hard coded values, when using
file
. once application "jarred", file system location no longer the same when running ide.if want read jar resource, should read resource via url,
getclass().getresource("...")
or 1 of variants.
also looks (with path using "resources/background"), "resource" on same level src
, , not event being included build path of jar. if extract jar, i'm pretty sure files won't there.
you should instead put "resources" inside "src", it's automatically put in build path. leave is, , configure build add it, take longer explanation. put in "src". have file structure like
projectroot src resources beackground
still problem trying read path file
, search file using on file system, using hard code relative path. won't work. said should read url. like:
url url = testfiles.class.getresource("/resources/background"); uri uri = url.touri(); file file = new file(uri); file[] files = file.listfiles(); (file f : files) { system.out.println(f.getabsolutepath()); }
this should work. make sure after build, files exist in jar, url of dir want, can create file
uri
- see
class
api more resource obtaining methods, , explanation.
Comments
Post a Comment