*
The moose likes Beginning Java and the fly likes Problem running jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem running jar" Watch "Problem running jar" New topic
Author

Problem running jar

King Goddard
Greenhorn

Joined: Nov 29, 2009
Posts: 11
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?


King Goddard
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
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

Joined: Aug 16, 2005
Posts: 14194
    
  20

"King Goddard", please check your private messages for an administrative message from JavaRanch.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
King Goddard
Greenhorn

Joined: Nov 29, 2009
Posts: 11
Thanks for the information I will save the for next time, but how would I load a file outside the jar?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Aug 16, 2005
Posts: 14194
    
  20

"King Goddard" - Please don't ignore the private message.
King Goddard
Greenhorn

Joined: Nov 29, 2009
Posts: 11
my name is real. I know it is cool people always think it is a nickname.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
King Goddard
Greenhorn

Joined: Nov 29, 2009
Posts: 11
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

Joined: Aug 16, 2005
Posts: 14194
    
  20

Ok King, I've seen it, no need to reply to the PM.
King Goddard
Greenhorn

Joined: Nov 29, 2009
Posts: 11
So does anyone know how to get the path of where ever the jar is currently located?
King Goddard
Greenhorn

Joined: Nov 29, 2009
Posts: 11
Never-mind I got it.
 
GeeCON Prague 2014
 
subject: Problem running jar