Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (496)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  file path that works in both Applets and Applications  (Read 1844 times)
0 Members and 1 Guest are viewing this topic.
Offline h3ckboy

JGO Coder


Medals: 5



« Posted 2012-04-29 19:11:27 »

Hello, I have been working on a game recently, and I have tried to make it run both as an applet and an application, I have been able to overcome most of the problems, but one I could solve but it would be inelegant.

The problem is that when I run as an applet (this is all in eclipse btw) it treats the root folder as /bin, but when I run it as an application it treats the root as being the project folder. Is there a way to make both of them the same in a way that will work with jar files nicely.

thank you, and sorry if I wasn't very clear

h3ckboy
Offline jonjava
« Reply #1 - Posted 2012-04-29 19:17:25 »

I just recently had a similar problem. And I also think Sproingle(sp?)'s new FAQ thread covered this (can't find the link).

The solution that worked for me was answered in the above thread with

1  
URL url = this.getClass().getClassLoader().getResource("resources/Ball.png");


The one in sprongles FAQ sounded like it was more reliable, it was something along these lines:

1  
Thead.getCurrent.load.... 


Oh god, it was nothing like that, but it started with Thread. :L

*searches for spoingle faq*

[EDIT]

Found it!
1  
2  
3  
4  
5  
6  
// APPROACH 1: To load from the root of jars in your classpath
Thread.currentThread().getContextClassLoader().getResourceAsStream("images/backgrounds/sky.jpg");

// APPROACH 2: To load resources relative to the package of this class (the class of 'this')
// e.g. if your class is org.stuff.Game, this will look for /org/stuff/config/settings.xml
getClass().getResourceAsStream("config/settings.xml");

Offline joeyismusic

Junior Member





« Reply #2 - Posted 2012-04-29 19:28:04 »

I add a top level folder named "res" and then add it to the project build as a class folder.

Then you use this line of code to grab em!

1  
TheClassWhereMainIs.class.getResource(filename)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-04-29 19:33:29 »

getClass().getClassLoader().getResource<AsStream>(String) is more reliable. I believe Thread.currentThread().getContextClassLoader() doesn't work in Applets in the browser.

Also, it is best to put "res" under "src" so you can do getResource("/res/" + fileName) and when exporting, everything is neatly organized under a "res" folder.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #4 - Posted 2012-04-29 19:41:42 »

jonjava's way worked like a charm after i tweaked my code to accomodate.

@ra4king: I tried the code you gave, but it gave a compiler error at the <AsStream> part, it just wanted to create a local variable. Also as for putting resources in the src folder, I have tried this in the past, but for some reason it has only worked when I put it into the bin folder. If you know a way to fix this I would be happy Smiley.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #5 - Posted 2012-04-29 19:47:11 »

Actually I have come across a problem just now, what if I am trying to load a directory? There is one point where I have to find all the directories inside of a specific directory. here is the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
//important part
File dir = new File("bin/data/users/");
          File listDir[] = dir.listFiles();
//less important part
         for (int i = 0; i < listDir.length; i++) {
             if (listDir[i].isDirectory()) {
                component = new Button(new Rectangle(275,50+i*75,100,50),listDir[i].toString().substring(15),applet);
                  component.addActionListener(this);
                  components.add((Component)component);
             }
          }


most of this may not make sense, but the marked portion is what really matters, and what I am hoping to convert.

Thank you
h3ckboy
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2012-04-29 19:50:22 »

You can only list the contents of a directory in the file system, you can't list the contents of a partial url path within a jar or the classpath.

The best way is to have some kind of index file which lists which files to open and their relative path. That may be hand-written or generated by some kind of build script.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #7 - Posted 2012-04-29 19:52:07 »

Does this mean that I cannot make directories either?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2012-04-29 19:55:38 »

Not within the jar(s) you're running from, they're immutable when you're running from the classpath. Think of your games jars as your exe - it's fixed and doesn't change between users.

What are you trying to do? It sounds like you're approaching something from the wrong direction.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2012-04-29 20:07:32 »

@h3ckboy
The angle brackets were just me denoting that you can use either getResource(String) or getResourceAsStream(String) XD

And you shouldn't be putting anything under bin....ever.

And as others pointed out, you cannot list the contents of a "folder" that is within a JAR nor can you create folders inside the JAR. If you want to make a save folder, use the Users' home folder by getting the path from System.getProperty("user.home");

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

JGO Coder


Medals: 5



« Reply #10 - Posted 2012-04-29 20:26:55 »

I guess, that would work, I was just trying to keep it all as compact as possible.

haha, ok, ra4king that makes a lot more sense, i thought you were thinking something along the lines of ArrayList<String>() kind of thing.

thank you all once again Smiley.
Offline ReBirth
« Reply #11 - Posted 2012-04-30 01:54:22 »

@ra4king
I never make it work with res folder under src. Maybe you keep the compiled class into it? logically I think eclipse execute class on bin folder, that's why the res folder should be on.

Offline sproingie

JGO Kernel


Medals: 202



« Reply #12 - Posted 2012-04-30 05:06:14 »

Use a separate resource folder, mark it as a source folder in eclipse, and its contents will be copied into your jar.  Me, I'm used to using maven projects, where it does this for everything in src/main/resources by default.
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.

BurntPizza (14 views)
2014-09-19 03:14:18

Dwinin (33 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (80 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (62 views)
2014-09-08 06:06:29

BurntPizza (46 views)
2014-09-07 01:13:42

Longarmx (33 views)
2014-09-07 01:12:14

Longarmx (37 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!