Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] How do I load resources for Web start?

Started by JarradPiper Jun 22, 2011 at 11:21 AM 6 replies 3k views
Original Post
JarradPiper
JarradPiper
[font=verdana, sans-serif][size=2]I'm trying to get my first game type thing working with webstart.

At the moment my program appears briefly as a black screen and then shuts down straight away when I use webstart.

I'm quite sure i've got the security signatures, jar files and jnlp file right. As they probably would of popped up as exceptions.

I'm quite confident the problem is most likely loading resources:

fManager.loadSectorTypes("sectors01.txt");
fManager.loadLevel("level2.txt");
texture[0] = TextureLoader.getTexture("PNG", new FileInputStream("stonewall02.png"));
texture[1] = TextureLoader.getTexture("PNG", new FileInputStream("stonefloor01.png"));

Those are examples of the lines that load the data in my program.. Worked fine in netbeans with the files in the project folder.

What do I change these lines to, and where do I put them in relation to my jar files to get it working properly?

I've read that it could be Resources.class.getClassLoader().getResource("path/to/logo.png"); however i'm not sure where the Resources object is.

Thanks for your help![/font]
The_Neverending_Loop
The_Neverending_Loop
You dont want to use FileInputStream like your normally would in this case, first Iam assuming you packaged all your resources into a JAR, so after you package your resources into a jar
you want to do something like this (If memory serves well)


ClassLoader loader = getClass().getClassLoader();
loader .getResourceAsStream("resourceInJar.png");

the loader .getResourceAsStream("") will return an inputstream which you can then feed into one of the input streams you want to use.


The resource being contained in the jar itself, I remember what I would do is package all my resources into one jar, while my code lives comfortably in another, and in the webstart file I specify these are the jars to use. Java should take care of the rest.

It has been a while since I created a webstart application tho, so It is a little fuzzy, but if you need anymore help (or some of this stuff doesnt work) let me know
Glass_Knife
Glass_Knife
There are two ways you can get the data from a *.jar file that I've used in the past:


URL url = getClass().getResource( "/" + fileName );
ImageIcon image = new ImageIcon( Toolkit.getDefaultToolkit().getImage( url ) );

// or

InputStream inputStream = getClass().getResourceAsStream( "/" + fileName );



Notice that in both cases the file is in the root folder of the jar file and you append the "/" to the front of the file name.
Also, remember that Java file names are case sensitive (which has bitten me on windows).

Now the problem with doing it this way is that when you try and use the code from netbeans, it won't find the files unless you have
a jar file with the flies in the classpath. What I've done in the past is some extra code to try loading the resources both ways...



InputStream file = loadFileFromJar( "fileName.txt" );
if( file == null ) {
file = loadLocalFile( "./res/models/fileName.txt" );
}



On one project I went so far as to create a library that tried loading resources multiple ways before giving up.

Enjoy!
JarradPiper
JarradPiper
Thanks guys.

As it stands I couldn't get this working. Heres the additions I made based on a boolean flag for selecting webStart.



if (useWebStart == true) {

InputStream inputStream = getClass().getResourceAsStream( "/" + "stonewall02.png" );
InputStream inputStream2 = getClass().getResourceAsStream( "/" + "dirt.png" );
InputStream inputStream3 = getClass().getResourceAsStream( "/" + "stonefloor03.png" );

texture[0] = TextureLoader.getTexture("PNG", inputStream);
texture[1] = TextureLoader.getTexture("PNG", inputStream2);
texture[2] = TextureLoader.getTexture("PNG", inputStream3);
}

if (useWebStart == false ) {
texture[0] = TextureLoader.getTexture("PNG", new FileInputStream("stonewall02.png"));
texture[1] = TextureLoader.getTexture("PNG", new FileInputStream("dirt.png"));
texture[2] = TextureLoader.getTexture("PNG", new FileInputStream("stonefloor03.png"));
}




File typeFile = new File(filename);
InputStream inputstream = getClass().getResourceAsStream( "/" + filename );


if (useWebStart == false) {
fileReader = new Scanner(typeFile);
}

if (useWebStart == true) {
fileReader = new Scanner(inputstream);
}



These are the only places in the program that were accessing resource files... I can't imagine what's wrong except perhaps the Slick-util texture loader was expecting a fileInputStream and/or maybe the scanner which I hadn't mentioned the first time should be something different.

Any more ideas? Thanks I do appreciate the help.
Glass_Knife
Glass_Knife
At this point, I would suggest checking out the Java Console. Configure Java to launch the Console when web start launches the application, and then add some debugging statements to zero in on the problem. I am using the code I posted in software that runs today, so I know it works.

And looking at your code, I don't see anything weird. Maybe there is a problem with the texture library you are using and the file being in the jar file, but without more information there's no way to know.
JarradPiper
JarradPiper
Thanks for the tip.

I dug into some logs in the deployment folder and this is the error message.

java.io.FileNotFoundException: http:\www.mydomain.com\javatest\gameresources.jar!\stonewall02.png (The filename, directory name, or volume label syntax is incorrect)

This means it isn't the scanner. It's the texture loading.

I Just have to now figure out how to alter this line of code to work with web start

[color=#1C2837][size=2][color=#000000]texture[color=#666600][[color=#006666]0[color=#666600]][color=#000000] [color=#666600]=[color=#000000] [color=#660066]TextureLoader[color=#666600].[color=#000000]getTexture[color=#666600]([color=#008800]"PNG"[color=#666600],[color=#000000] [color=#000088]new[color=#000000] [color=#660066]FileInputStream[color=#666600]([color=#008800]"stonewall02.png"[color=#666600]));


Glass_Knife
Glass_Knife

Thanks for the tip.

I dug into some logs in the deployment folder and this is the error message.

java.io.FileNotFoundException: http:\www.mydomain.com\javatest\gameresources.jar!\stonewall02.png (The filename, directory name, or volume label syntax is incorrect)

This means it isn't the scanner. It's the texture loading.

I Just have to now figure out how to alter this line of code to work with web start

[color="#1C2837"][color="#000000"]texture[color="#666600"][[color="#006666"]0[color="#666600"]] [color="#666600"]= [color="#660066"]TextureLoader[color="#666600"].[color="#000000"]getTexture[color="#666600"]([color="#008800"]"PNG"[color="#666600"], [color="#000088"]new [color="#660066"]FileInputStream[color="#666600"]([color="#008800"]"stonewall02.png"[color="#666600"]));



Did you launch the game from the jar file by just clicking on it and running it, not using webstart? It looks like it should be finding the file. Make sure that just running the application finds the image.
If that works, then maybe there is a problem with the jar file not being signed, or something like that.


JarradPiper
JarradPiper
Thanks mate.

What I ended up doing was ditching the Slick-util in favor of another texture loader borrowed from the space invaders example game for LWJGL.


http://www.lwjgl.org/wiki/index.php?title=Space_Invaders_Example_Game

After that the same command as the scanner worked for the imageIO.read() method in the new texture loader.

Thats what I reccomend to any people who find this thread, having the same trouble.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.