tags:

views:

266

answers:

3

I have a method that gets called at the start of my program, and it works fine when I run the jar, but when I run the jnlp file it crashes.

public void newImg(String i)
{
    try
    {
        File file = new File(i);
        img = ImageIO.read(file);
    }
}

img is a bufferedImage, by the way.

+2  A: 

Odds are the path you're giving to the file, or permissions, don't match. Put the File ctor into a try block, catch the exception, and find out what it is.

It should look something like this:

try {
    File file =  new File(i);
    img = ImageIO.read(file);
} catch (Exception ex) {
    // You probably want to open the java console, or use a logger
    // as a JNLP may send stderr someplace weird.
    Systemm.err.println("Exception was: ", ex.toString());
}

The code you have doesn't do anything with the exception.

You might want to look at the exceptions thread in the Java Tutorial.

update

See my comments. I just tried something and confirmed what I was thinking -- code with a try {} and no catch or finally won't even compile. if this is really the code you think you're working with, you've probably been loading an old class file; this one hasn't compiled.

$ cat Foo.java 
public class Foo {
     public void tryit() {
         try {
             File f = new File(null);
         }
     }
}
$ javac Foo.java
Foo.java:3: 'try' without 'catch' or 'finally'
         try {
         ^
1 error
$
Charlie Martin
How could the file be right in the jar, but wrong when I use the jnlp?
William
The pathname could be a relative one, the JNLP could be running with different permissions so the file's not accessible, or it could be failing for some reason in the read. the File ctor only throws one exception, a NullPointerException if the argument is null, so ...
Charlie Martin
... it might be that the string argument isn't what you think. But the code you exhibited isn't correct in any case (no catch). Try the change I suggested and see what you find out. Oh, and what's the JVM version, the OS, and the hardware architecture?
Charlie Martin
+1  A: 

Maybe your not loading your image properly. Don't use the relative location of the file. This will be different for each OS. Your image in the JAR you should be loaded correctly like this:

URL url = this.getClass().getResource("image.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);

This will load a file called image.jpg that is located in the same location as the class. You can also use things like File.pathSeparator if its in another location.

Use one of these two methods to load it as a resource:

http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String) http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

Bernie Perez
How do I load a bufferedImage from that though?
William
Well once you have an image object you can create a bufferedImage object with this:http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html?l=relIt will create a graphics object and draw it onto the bufferedImage. Also has support for things like the Alpha channel.
Bernie Perez
A: 

Make sure you have the correct file name/path.

Make sure you have file access to the file system.

l_39217_l