Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Browsing files in jar and outside jar  (Read 1262 times)
0 Members and 1 Guest are viewing this topic.
Offline z.e.r.o

Junior Member




Java games rock!


« Posted 2004-03-09 05:17:57 »

Ok, in my solitaire software (version 0.3 will be public soon) I wanto to use multiple cards and deck backside. The technique I used to provide themes i pretty simple: for cards every subdirectory in imgs/cards is a deck theme, and for the cards backside the program should look in imgs/backs.

The only problem is that, after a week of tampering I was unable to browse effectively the files available in the jar and othside the Jar.

(In the Jar)
I can access single reources with ClassLoader.getResource() and getResourceAsStream() but as far I tried I cannot use File to enumerate the files in a directory (for example to browse the backsides...).
Another thing I noticed in many other apps for java is that usually the program can check for data in the folders in the jar or in a same folder placed at the jar level where the application is installed (since my solitary architecture is pluggable this is a must to allow others to add their solitaires preserving the application integrity).

(Outside Jar)
Another thing that bothers me is that File seems not be able to resolve relative pathnames in Windows, no matter i use "imgs\\" or ".\\imgs\\" to locate the imgs directory it will fail always. Only if I provide the integral pathname of the directory it will locate it and let me browse files.
I tried system preferences to retrieve the path where the application is installed but the only place is store is in the classpath, togheter with other directories (and how the program will know where the user put it?).

Matteo Anelli
.brain - http://www.dot-brain.com
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2004-03-09 05:35:07 »

Okay, inside the jar you need to open a JarInputStream and parse it manually.  The URL returned from getResource isnt a file URL in this case, its a jar entry URL which is why you cannot open it as a file.  Forget getResource and scan the jar yourself.  OR put a text list of your files in a well known place so you can use getResource to get it and then use it to get the rest.

Outside the jar you need to use System.getProperty("user.dir") to get the CWD and append it to your file path.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #2 - Posted 2004-03-09 09:03:47 »

Quote

Outside the jar you need to use System.getProperty("user.dir") to get the CWD and append it to your file path.


Huh? That returns the CWD?

More to the point, why isn't the CWD correctly set automatically? I know it is for a "normal" installation, and the only times I remember problems with this were e.g. with J2EE implementations that started java from some weird and wonderful places, and loaded your WAR/JAR from somewhere else entirely.

malloc will be first against the wall when the revolution comes...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #3 - Posted 2004-03-09 09:09:55 »

Quote

The only problem is that, after a week of tampering I was unable to browse effectively the files available in the jar


Is there an RFE for this? I'd actually thought it was in the standard libraries by now (1.5?) but I haven't looked at it in ages...

malloc will be first against the wall when the revolution comes...
Offline z.e.r.o

Junior Member




Java games rock!


« Reply #4 - Posted 2004-03-09 12:06:06 »

Thanks Jeff, it works.

But only if I run the JRE in the console from the same directory of the jar/class files, otherwise it returns the working path of the java executable.
It works also if I prepare a shortcut with the given working directory in Windows.

Any deployment hints? Will Webstart set it up by itself the right way (cannot try because don't have a web server configured to handle jnlp properly right now)?
I have to provide some installation/running script (that can be broken easily and is hard to work with for the average user - expecially for Windows users -)?

Matteo Anelli
.brain - http://www.dot-brain.com
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2004-03-09 21:12:25 »

Quote
Thanks Jeff, it works.

But only if I run the JRE in the console from the same directory of the jar/class files, otherwise it returns the working path of the java executable.
It works also if I prepare a shortcut with the given working directory in Windows.


Right because if you double click a document in Windows, the CWD is set to the dir of the executing program.

So yes if you don't do something to make sure your CWD is set right (eg set by a shortcut or run from the console after you CD to the app dir) you wont get the right answer.

GIGO  Smiley


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #6 - Posted 2004-03-09 21:20:20 »

Wouldn't it be great if there was a Java API to change the CWD? Smiley

Offline z.e.r.o

Junior Member




Java games rock!


« Reply #7 - Posted 2004-03-09 21:48:34 »

...or just an API to retrieve the path of the class starting the application (assuming that as application root)

...or the path of the class file the current instance is based of

I must also admit that knowing too much on the application surroundings can be prone to security exploits if the application is allowed to have unlimited control on the machine.

Doh! for example an app with a pluggable interface (like mine) can be prone to maliciuos pluggable classes (maybe is better using a data file for defining solitaires and a simple editor???)

Matteo Anelli
.brain - http://www.dot-brain.com
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #8 - Posted 2004-03-09 23:02:04 »

These are all things that Tomcat users were begging for circa 4 years ago (TC was a proverbial steaming pile back then, and sooooo many people couldn't run servlets etc because of these CWD issues).

Hence I'd assumed Something Had Been Done by now...

malloc will be first against the wall when the revolution comes...
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.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (247 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!