Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  Loading files after export  (Read 612 times)
0 Members and 1 Guest are viewing this topic.
Offline blaa_vogn

Junior Devvie


Projects: 2



« Posted 2013-02-27 17:58:45 »

Hi everybody

I am trying to make a game, and have run into a problem.

I have made a game, which consists of several levels.
Each level is saved as an xml-file, which is loaded and parsed.
1  
loadLevel(new File("resources/levels/"+levelID));


Everything has been working until I tried to export the program, after exporting the program I can't find a way to load my level-files.
I have tried to place my files within the game-jar, and I have tried to place them at the same level as my jar-file, but I can't seem to reach them.

So what am I doing wrong?

Best wishes
Blaa Vogn
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2013-02-27 18:26:11 »

That's because you're using File, which doesn't work inside a jar.  You need to access it as a resource instead.  

http://www.java-gaming.org/topics/jgo-frequently-asked-questions/26316/view.html#post_how_do_i_load_resources

If you're using libGDX, use Gdx.files.internal instead.  Slick also has its own resource loader, though I'm not sure you're any better off using it.

Offline blaa_vogn

Junior Devvie


Projects: 2



« Reply #2 - Posted 2013-02-28 15:44:02 »

Thank you very much it is working, and I only need music for my game to be complete Cheesy.

My only problem is now, that when I try to compile and run the program with Eclipse, I can't find the files.

I guess my project has the wrong configuration, I have tried to include the resource folder to the "Java build path", but that doesn't help. Any ideas?

I could just either export the game each time I want to try it, or run with two different version, but it seems rather frustrating.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-02-28 15:50:09 »

Project/
../bin/
../res/
../res/myres_name_blabla.txt
../src/

Include "res" into java build path and use

1  
URL url = this.getClass().getClassLoader().getResource( "myres_name_blabla.txt" );


http://www.java-gaming.org/topics/how-to-get-the-right-path/28848/msg/263379/view.html

Offline blaa_vogn

Junior Devvie


Projects: 2



« Reply #4 - Posted 2013-02-28 19:32:01 »

Okay, it is working.

I don't quite understand why "myres_name_blabla.txt", is correct and "res/myres_name_blabla.txt" isn't, but it is working!
Online cylab

JGO Ninja


Medals: 55



« Reply #5 - Posted 2013-02-28 19:40:23 »

because if you add res to your build path, it takes the contents of res and puts them in the jar...

Mathias - I Know What [you] Did Last Summer!
Offline jonjava
« Reply #6 - Posted 2013-03-01 00:41:54 »

Indeed, since both src (by default) and now res are in the build path, their CONTENTS will be put into the jar.

If you would have created a folder under "src" (src/res/my_res.txt, for example) then "res/my_res.txt" would work because res is now part of src's CONTENTS (and res would no longer be in the build path directly).

Offline Icecore

Senior Devvie


Medals: 5



« Reply #7 - Posted 2013-03-01 01:25:38 »

Quote
Project/
../bin/
../bin/res/myres_IN.txt // auto moved here from /src/ when run application (Eclipse)
../res/
../res/myres_Out.txt
../src/
../src/res/myres_IN.txt

i use this =)
for in jar - wher: path = res/myres_IN.txt (../src/res/myres_IN.txt)
1  
2  
3  
      ClassLoader c = ClassLoader.getSystemClassLoader();
      InputStream is = c.getSystemResourceAsStream(path);
      if(is == null){throw new FileNotFoundException(path);}



and for out - wher: path = res/myres_Out.txt (../res/myres_Out.txt)
// need move res folder near jar when export it
out have bug - need run like "java -jar blabla.jar"
if you run from out folder like "java -jar folder_A/blabla.jar" (C:/folder_0/folder_A/blabla.jar)
java search "res" in "folder_0" not wher jar itself  (not in "folder_A")^^
1  
2  
3  
      File file = (new File(path));   
      if(!file.exists()){throw new FileNotFoundException(path);}
      InputStream is = new FileInputStream(file);

i prefer out.
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.

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

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

toopeicgaming1999 (10 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (27 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
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!