• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem running jar

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using NetBeans 8 and it works great. I have successfully designed my first GUI program written by myself, but I ran into an annoying problem that makes me wanna smash my computer. I can run my program in NetBeans no problem, but after I build and attempt to run it it doesn't work.

I know what the problem is I just don't know how to fix it.

I access files that are outside of my jar file and usually in an IDE if you put the files in the same directory it operates file and you can access those files by simply typing in the file name, but if you attempt to do the same after it has been compiled I get an expected response of file not found which is my error catcher.

Now how can I make sure the files I am needing to access can always be found relative to where my jar file is?
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

King Goddard wrote:Now how can I make sure the files I am needing to access can always be found relative to where my jar file is?


How about putting it inside the jar file?

prompt-> jar -tf foo.jar
META-INF/
META-INF/MANIFEST.MF
foo/
foo/bar.txt
foo/LoadFileFromJar.class


I put my file bar.txt right beside my class that read it.
My code looks like (not too pretty though)
Eclipse did not want to build the jar for me so I had to build it myself. (Due to my ignorance.)

jar -cef foo\LoadFileFromJar foo.jar foo


And it runs with

java -jar foo.jar foo.LoadFileFromJar



You can also check out the thread Just-Another-Resource.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"King Goddard", please check your private messages for an administrative message from JavaRanch.
 
King Goddard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information I will save the for next time, but how would I load a file outside the jar?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are actually doing in your IDE is loading the files from the current working directory, not loading them from the same directory where the jar is located. So I recommend you continue to do that when you leave the cradle which your IDE is.

That means you should set your working directory before you run the jar. But as Jonas says, it's better to put the resources inside the jar as well as the code.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"King Goddard" - Please don't ignore the private message.
 
King Goddard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my name is real. I know it is cool people always think it is a nickname.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps it would have been a good idea to reply to the message, stating that it is real.
 
King Goddard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Perhaps it would have been a good idea to reply to the message, stating that it is real.



You guys are kind of domineering....hahah I did. Just doing both.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok King, I've seen it, no need to reply to the PM.
 
King Goddard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So does anyone know how to get the path of where ever the jar is currently located?
 
King Goddard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never-mind I got it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic