Problem packaging .jar file

Hi there, I have some problems deploying my application with jar files, here’s my directory structure:

  1. /dactylo/ - here reside every .class file
  2. /data/ - text files with data in it like the properties file
  3. /data/lessons - text files with lessons needed
  4. /gfx/ - all the images are in here

My program is packaged in “package dactylo;” and when I start my program in eclipse, everything works fine.

I got the following methods accessing components outside the package:

  1. I read the content of /data/lessons with:

File dir = new File(“data/lessons”);

  1. I read several images all using

Toolkit.getDefaultToolkit().getImage(“gfx/image_name”);

  1. I read a properties file using:

properties.load(new FileInputStream(“data/dactylo.properties”));

My question is, when I package everything inside a .jar file, the images won’t load, the File returns a NullPointerException and the property file doesn’t load. I know there is a problem with relative filenames and stuff but I don’t know how to resolve the problem.

When I copy the folders data and gfx to the jar file, everything works fine, but I want to use webstart to deploy my software and as I know, everything has to be inside jar files to actually make them load.

I’d really appreciate every help I get :smiley:

Use this.

//Load image using ImageIO
public BufferedImage loadImage(String fileName) {
try{
return ImageIO.read( this.getClass().getResourceAsStream(fileName) );
}
catch(IOException e){}
return null;
}

Read prop
properties.load(this.class.getResourceAsStream
(“data/dactylo.properties”));

If you need to read a dir in a jar file you need to use GzipInputstream to read the zip file and iterator all entries in the directory of the zip file. Search the forum for some code on that.

Yep, what Backmask said :slight_smile:
Here is a good overview of webstart: http://grexengine.com/sections/externalgames/articles/Kevin%20Glass-A%20Walkthrough%20with%20WebStart-JNLP-1.html

Good luck! //
Gregof

[quote]Yep, what Backmask said :slight_smile:
Here is a good overview of webstart: http://grexengine.com/sections/externalgames/articles/Kevin%20Glass-A%20Walkthrough%20with%20WebStart-JNLP-1.html

Good luck! //
Gregof
[/quote]
Should now be:

http://javagamesfactory.com/articles/Kevin%20Glass-A%20Walkthrough%20with%20WebStart-JNLP-1.html

(both point to the same place)

Also, there will be a major rewriter of that article done at some point. I think Kev’s too busy, and it might not be until Xmas, so don’t hold your breath :slight_smile:

Thanks a lot for the great answers, I only got 1 little problem left.

I’ve got my application working using webstart and everything works just fine. When i start the .jnlp file localy, everything works just as expected.

But when I upload it onto the server, the following happens:

I use this code to read the jar file:

FileInputStream fis = new FileInputStream(“lessons.jar”);

when I start the .jnlp localy, the jar file is read and everything is ok.

When I upload it, he can’t seem to find the .jar file.

I used the following .jnlp file:


<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<jnlp codebase="http://localhost/dactylo/"
href="http://localhost/dactylo/dactylo.jnlp">
  <information>
    <title>Dactylo version 0.2</title>
    <vendor>Laurent Haan</vendor>
    <description>Dactylo Game for myschool</description> 
    <offline-allowed/>
  </information>
  <security>
    <all-permissions/> 
  </security>
  <resources>
    <j2se version="1.1+"/>
    <jar href="dactylo.jar" download="eager"/>
    <jar href="lessons.jar" download="eager"/>
  </resources>
  <application-desc main-class="dactylo.Dactylo"/>
</jnlp>

I am running a local apache and added the MIME type for the .jnlp file.

Did I miss something ?

Thanks a lot !

Update:

I changed the code a bit and now I can put everything in a single .jar file called dactylo.jar.

Everything works just fine now, but I’ve got the same problem as above. When I write:

JarFile jarfile = new JarFile(“dactylo.jar”);

then he can’t find the file dactylo.jar when I start it on the webserver.

Any idea ?

You can’t have a FileInputStream, it tries to read from a file on disk, when you use webstart you have to read all your resources as streams, like in the image-example above, try: InputStream fis = this.getClass().getResourceAsStream("/lessons.jar");

// Gregof

Thanks a lot for all your kind words and your help, now everything is working just fine.

Thanks !