Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED]how to load file depending on the folder where the Jar exists  (Read 2223 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-05-11 19:42:04 »

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

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Online HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-05-11 19:59:17 »

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

Offline alaslipknot
« Reply #2 - Posted 2013-05-11 20:01:09 »

THANK YOU  Grin

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-05-11 21:48:55 »

You know, there's a thing called google...
Offline Zhon

Junior Devvie


Medals: 4



« Reply #4 - Posted 2013-05-11 22:13:20 »

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");
Offline Sammidysam
« Reply #5 - Posted 2013-05-11 23:45:52 »

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


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?
Online HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #6 - Posted 2013-05-11 23:52:06 »

Are you sure you don't mean
System.getProperty("user.home")

Offline ReBirth
« Reply #7 - Posted 2013-05-12 02:22:04 »

AFAIK user.home returns ~ on linux.

Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2013-05-12 06:47:33 »

@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.

Offline ReBirth
« Reply #9 - Posted 2013-05-12 23:35:49 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #10 - Posted 2013-05-13 05:17:33 »

@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.

Offline nsigma
« Reply #11 - Posted 2013-05-13 08:42:45 »

This could actually just be an OpenJDK error
I could never reproduce that quirk.

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
SomeClass.class.getProtectionDomain().getCodeSource().getLocation()
.  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

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Online HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #12 - Posted 2013-05-13 08:55:05 »

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?

Offline nsigma
« Reply #13 - Posted 2013-05-13 09:00:41 »

It's usually safe to assume the jar will be launched from the directory it is in.
...
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!  Grin

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!  Tongue

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2013-05-13 20:18:53 »

Then you should just rely on getClass().getClassLoader().getResource<AsStream>(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<AsStream>(String).

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

Offline Sammidysam
« Reply #15 - Posted 2013-05-13 21:06:52 »

Thanks.  I will need to look into my programs to fix this, as I don't like populating ~.
Offline nsigma
« Reply #16 - Posted 2013-05-14 10:37:21 »

Then you should just rely on getClass().getClassLoader().getResource<AsStream>(String). That's the best option as it loads relative to the root of the JAR.

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.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Online HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #17 - Posted 2013-05-18 00:17:48 »

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.

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


It returns the directory containing the jar.

Offline alaslipknot
« Reply #18 - Posted 2013-05-18 01:44:32 »

^ thanx

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline nsigma
« Reply #19 - Posted 2013-05-18 10:28:07 »

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.

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


It returns the directory containing the jar.

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?

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Online HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #20 - Posted 2013-05-18 19:20:49 »

1. Sorry. That was left in while 'experimenting'. That was there before I tried making it a file, and I accidentally left it in.

2. I need the path as a string to get files from the directory (and subdirectories)

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (35 views)
2014-12-09 22:41:13

BurntPizza (70 views)
2014-12-08 04:46:31

JscottyBieshaar (32 views)
2014-12-05 12:39:02

SHC (44 views)
2014-12-03 16:27:13

CopyableCougar4 (40 views)
2014-11-29 21:32:03

toopeicgaming1999 (108 views)
2014-11-26 15:22:04

toopeicgaming1999 (94 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!