[SOLVED]how to load file depending on the folder where the Jar exists

hello,
am making a game where you can create and load levels, i couldn’t find a way how to embed the level files (txt) into the exported jar (if anyone knows how it would be better i think) but for now in order to play the game the level should be in “C:\JavaGame”
what i want is how to load them depending on the folder where the Jar exists
for example if your jar is in “C:\Game” then the game will look into “C:\Game\JavaGame”
i hope i explained well
thank you


System.getProperty("user.dir") + path + to + your + file;

THANK YOU ;D

You know, there’s a thing called google…

If the text file is in the same jar you’re running why do you want to find the folder/location where is it?
You could use SomeClass.class.getClassLaoder().getResourceAsStream(“res/sometext.txt”);

I’ve found that System.getProperty(“user.dir”) returns the home directory on Ubuntu (so if your jar is in /home/sam/Documents/PixelZombies/ it will return /home/sam/) unless you go to Terminal and cd to the desired directory and then run the jar via the Terminal. So if you right click the JAR and click “Run in OpenJDK Runtime” (I believe that is what it is, but I’m not on Ubuntu right now) it will not return the right value. This could actually just be an OpenJDK error, but if it isn’t, is there a better way to get the same result?

Are you sure you don’t mean [icode]System.getProperty(“user.home”)[/icode]

AFAIK user.home returns ~ on linux.

@Rebirth
~ == the home directory

@Sammidysam
With much fiddling, I could never reproduce that quirk. I was always given the correct current directory of the program in Ubuntu.

@ra4king
Ya of course I know (I use single linux). But HGD gave that solution which is kinda off.

I was responding to @SammidySam’s problem with user.dir returning the home directory and wondering if he was using user.home by mistake.

You guys are misinterpreting what the user.dir property is meant to represent. It is the current working directory, not the directory where the program resides. For example, if you run a command in the shell (eg. ls) you want it to list the files in the working directory, not the directory where the ls command resides. If anything’s quirky it’s probably Windows Java behaviour here.

One way to answer the OP’s question may be to use [icode]SomeClass.class.getProtectionDomain().getCodeSource().getLocation()[/icode]. Obviously SomeClass should be a class in the JAR you’re looking for.

See more http://stackoverflow.com/questions/2837263/how-do-i-get-the-directory-that-the-currently-executing-jar-file-is-in

It’s usually safe to assume the jar will be launched from the directory it is in.
At least on Windows.

On Linux (I tested too) it does seem to give strange results from user.dir when you don’t launch from the terminal.
Is there a way to fix this or do we have to stop relying on user.dir?

To assume makes an ass of u and me! ;D

Don’t assume that the JAR is launched from the directory it is in. That assumption will break in interesting ways. If your program relies on the directory its JAR is in and not the directory the user is in, don’t use user.dir - there’s a clue in the name! :stuck_out_tongue:

Then you should just rely on getClass().getClassLoader().getResource(String). That’s the best option as it loads relative to the root of the JAR. If you want to load relative to the Class returned by getClass(), then drop the “getClassLoader()” call to make it getClass().getResource(String).

Note the denotes that it’s optional as there are 2 methods: getResource(String) which returns URL and getResourceAsStream(String) which returns an InputStream.

Thanks. I will need to look into my programs to fix this, as I don’t like populating ~.

Which isn’t the same thing!

Mind you, as the OP says, he might be better off putting the level files in the JAR - depends if he wants it to be user editable / extendable.

Okay, I had a bit of trouble fixing Guardian II using the code mentioned above, and decided to post the fix I found in case someone has the same problem.


String jarDirectory = new File(SomeClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParent();

It returns the directory containing the jar.

^ thanx

For a moment I thought you were just repeating what I wrote above :wink: , but I see what you’re trying to do.

Two questions (not tried it)

  • why use getPath() rather than construct the File from the URI returned from toURI()?
  • why not use getParentFile() to return a File representing the directory?