Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  accessing dirs above ClassLoader's dir  (Read 2790 times)
0 Members and 1 Guest are viewing this topic.
Offline Kova

Senior Devvie





« Posted 2007-06-05 13:51:41 »

Hello.
How can I cleanly access dirs if they are above ClassLoader directory? For example:
working directory:
    -classes
        -org.something.Class
    -models
    -textures
    -.....

With getClassLoader().getResource() I can only get to "classes" directory, not in working directory. Putting working directory in classpath at runtime is not desired.
Thank you.
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #1 - Posted 2007-06-05 14:35:40 »

With File/FileInputStream... well, don't do that.

-dist
--org
---something
----Class
--models
--textures
--.....

or

-dist
--org
---something
----Class
--media
---models
---textures
---.....

The advantage is that you can use webstart if you like.

弾幕 ☆ @mahonnaiseblog
Offline mickleness

Senior Newbie





« Reply #2 - Posted 2007-06-05 19:30:55 »

Also depending on your situation you might try looking at:
System.getProperty("java.class.path")

Note this may return multiple paths, so you'll have to parse those paths according to File.PATH_SEPARATOR or something similar.

Whether you're interested in this depends a lot on what your situation is... (are you making a webstart app or a desktop application, for example?  Do you even have permissions to spelunk around the file system?)  Also I'm not sure what you mean by "cleanly accessing" the directories.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Devvie





« Reply #3 - Posted 2007-06-05 22:57:39 »

it's not actually for me, but my friend is developing java projects and he uses this approach (to have class files in directory classes, which is top most dir that ClassLoader gets) and resources in another directories, not under classes. So he can't use getResource() to get resources Smiley
by cleanly I meant with no very complicated solutions, hackery or platform dependand stuff.
Offline cylab

JGO Ninja


Medals: 55



« Reply #4 - Posted 2007-06-06 09:05:00 »

there is no clean way to archieve what he asks. either the directory layout has to be changed or the startup script has to make sure, that the root-directory of the project is the current working directory. then you can simple open a file with e.g. new FileInputStream("models/lara.m3d").

Mathias - I Know What [you] Did Last Summer!
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #5 - Posted 2007-06-06 10:45:04 »

Using some specific directory layout for distribution just because you think it's nicer...

Seriously... why?

If it's in jars you have like game.jar, models.jar, music.jar... and if only the game is in a jar, you'll have that single jar at the root and the other dirs next to it. Disabling the webstart option (which is truly great for beta-ing) just because you think some other layout is sexier is completely retarded. It's like using wav instead of ogg, because you can type "wav" using the left hand only.

Maybe he isn't aware that the dev and dist layouts don't need to be identical?

弾幕 ☆ @mahonnaiseblog
Offline Kova

Senior Devvie





« Reply #6 - Posted 2007-06-06 12:33:31 »

Using some specific directory layout for distribution just because you think it's nicer...
Seriously... why?

If it's in jars you have like game.jar, models.jar, music.jar... and if only the game is in a jar, you'll have that single jar at the root and the other dirs next to it. Disabling the webstart option (which is truly great for beta-ing) just because you think some other layout is sexier is completely retarded. It's like using wav instead of ogg, because you can type "wav" using the left hand only.

Maybe he isn't aware that the dev and dist layouts don't need to be identical?

I must say I agree it's nicer.. why? Becouse then all your packeges and classes don't interfear with other directories. Like if you have:

-org
--something
---Class ...
--sometthing2
--somethng2
-net
--something3
--something4
-info...
--blabla...
...a bunch of packages
-models
-textures

...so you see it's logical to have all classes in directory named classes. This way models and textures dir don't belong here... it's like they are part of packages. This is only for development of course, later you'll pack everything in a jar It doesn't matter much. I don't use this personally, but then again I never worked on bigger projects.
One more thing he is right about, why does java had to limit our choices anyway? Not best strategy...
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #7 - Posted 2007-06-06 13:49:15 »

>Becouse then all your packeges and classes don't interfear with other directories.

They don't either way.

>One more thing he is right about, why does java had to limit our choices anyway?

It doesn't. If you want to access your stuff through the classpath the stuff needs to be in the classpath (obviously). That's all.

弾幕 ☆ @mahonnaiseblog
Offline Kova

Senior Devvie





« Reply #8 - Posted 2007-06-06 15:58:21 »

the point is those directories don't logically belong there, they are not packeges

and you are right, but they could have made classpath more friendly to support this (don't ask me how exactly, I'm sure there is a simple way it could be done)
Offline cylab

JGO Ninja


Medals: 55



« Reply #9 - Posted 2007-06-06 16:01:37 »

Using some specific directory layout for distribution just because you think it's nicer...
Don't know if this was targeted at my reply. If so, I was misunderstood Wink

IMHO resources are tied to the application anyway, so the directory structure should be:

-org
   -yourorg
       -appname
            -resources
                 -models
                 -textures
            MainClass

this way everything is allways in the apps classpath and no interferance with the default package as well.

>One more thing he is right about, why does java had to limit our choices anyway?

It doesn't. If you want to access your stuff through the classpath the stuff needs to be in the classpath (obviously). That's all.

I agree. since the classpath consists of multiple locations it's simply impossible to resolve a directory "besides" your classes directory. If you want to work with files you still can usinf File and FileInputStream, but you are back to all the problems they have (working directory constraints, relative/absolute pathes, security concerns with webstart deployment etc.)

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline broumbroum

Junior Devvie





« Reply #10 - Posted 2007-06-06 16:20:45 »

You can find the complete classpath with getClasspath() from the new MXBeans available statically and then switch within the path with ";" chars-array to get one available directory if you don't want to add your own in the classpath.
then simply copy the lib files in them while you're loading your app-stuff and eventually delete them on exit. It is then affordable to loadLibrary with the System once the files have been copied in. Maybe you'll be asked to set the permissions on for each library you load, i.e. RuntimePermission loadLibrary yourLib .

I do like that for each one of my applications:( btw they use JAI+JIIO bin and natives depending the os.name system property)
See: getYourFiles FileArray[] --> check the classpath String for writable dir --> copy the fileArray[] one-by-one --> loadLibrary(myLib) 
and check before to set up your j2 permissions with policytool though this will be logged in the console if it is needed to.

 Roll Eyes

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline Kova

Senior Devvie





« Reply #11 - Posted 2007-06-06 16:55:43 »

-org
   -yourorg
       -appname
            -resources
                 -models
                 -textures
            MainClass

I agree, I work like this also, but I am dissapointed of lack of other solutions, like static approach we talk about here. Static means resources aren't tied to the app itself.
Offline cylab

JGO Ninja


Medals: 55



« Reply #12 - Posted 2007-06-06 23:09:28 »

I agree, I work like this also, but I am dissapointed of lack of other solutions, like static approach we talk about here. Static means resources aren't tied to the app itself.

Sorry, but I really don't get it. There are plenty of options:

- Use separate jars/directories and add them to the classpath
- Use separate jars/directories and create a URLClassloader yourself at runtime
- Use direct file access taking the current working directory into account (startup-script, lnk-file etc.)
- Traverse the classpath and find the media directories yourself (hackish)

what's missing?

Mathias - I Know What [you] Did Last Summer!
Offline Kova

Senior Devvie





« Reply #13 - Posted 2007-06-06 23:45:58 »

- Use separate jars/directories and add them to the classpath
- Use separate jars/directories and create a URLClassloader yourself at runtime
- Use direct file access taking the current working directory into account (startup-script, lnk-file etc.)
- Traverse the classpath and find the media directories yourself (hackish)

again, it's for development time, so jars are excluded. Adding directories to classpath every time you make one is also excluded. Soulution I was thinking of is programatical, if that is right word for it.
Direct access with streams seems to be only adequate solution (imho).
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #14 - Posted 2007-06-07 00:38:43 »

Well, add the upper dir to the cp and you're done.

弾幕 ☆ @mahonnaiseblog
Offline Kova

Senior Devvie





« Reply #15 - Posted 2007-06-07 10:07:49 »

Well, add the upper dir to the cp and you're done.

Yeah that would be nice!
I've tried to do that, but how do you do that exactly? So here's the dir tree:

project
-classes
--something
---PathExample.class
-data
--data.dat

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
package something;
public class PathExample {
   public static void main(String args[]) {
      new PathExample();
   }  
   
   public PathExample() {
      java.net.URL data = this.getClass().getClassLoader().getResource("data/data.dat");
      System.out.println(data);
   }
}


so I'm in project directory. I've tried to use "java -cp . classes/something.PathExample" but got NoClassDefFoundError... (wrong name: something/PathExample).
I'm totally new here, never tried something like this. Can anyone tell me how to access resources in classpath? I know that getResource() starts in dir where packages are, but what for directories added to classpath?
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #16 - Posted 2007-06-07 12:22:40 »

You could either start from "project" and add "classes" to the cp. Or if you start from "classes" you would have to add "../" (I think) to the cp. I would go for the former.

java -cp .;classes something.PathExample

data.dat would be accessed with "/data/data.dat".

弾幕 ☆ @mahonnaiseblog
Offline Kova

Senior Devvie





« Reply #17 - Posted 2007-06-07 20:31:20 »

thank you, that works

but what about uniqueness? Is it up to me to preserve it? For example if I add 2 directories that contain same file by name, which one will be fetched?
Offline Mr_Light

Senior Devvie


Medals: 1


shiny.


« Reply #18 - Posted 2007-06-08 07:29:25 »

I would really just use maven.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
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.

Mr.CodeIt (23 views)
2014-12-23 03:34:11

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

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

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

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

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

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

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

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

toopeicgaming1999 (151 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

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