Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem running jar

 
King Goddard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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?
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
my name is real. I know it is cool people always think it is a nickname.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Never-mind I got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic