0

I'm having a weird problem in java. I want to create a runnable jar: This is my only class:

public class Launcher {

public Launcher() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {
    String path = Launcher.class.getResource("/1.png").getFile();
    File f = new File(path);
    JOptionPane.showMessageDialog(null,Boolean.toString(f.exists()));

}

}

As you can see it just outputs if it can find the file or not. It works fine under eclipse (returns true). i've created a source folder resources with the image 1.png. (resource folder is added to source in build path)

As soon as I export the project to a runnable jar and launch it, it returns false. I don't know why. Somebody has an idea? Thanks in advance

edit: I followed example 2 to create the resources folder: Eclipse exported Runnable JAR not showing images

Community
  • 1
  • 1
Bosiwow
  • 2,025
  • 3
  • 28
  • 46

4 Answers4

2

If you would like to load resources from your .jar file use getClass().getResource(). That returns a URL with correct path.

Image icon = ImageIO.read(getClass().getResource("image´s path"));

To access images in a jar, use Class.getResource().

I typically do something like this:

InputStream stream = MyClass.class.getResourceAsStream("Icon.png");
if(stream == null) {
   throw new RuntimeException("Icon.png not found.");
}

try {
   return ImageIO.read(stream);
} catch (IOException e) {
   throw new RuntimeException(e);
} finally {
   try {
      stream.close();
   } catch(IOException e) { }
}

Still you're understand, Kindly go through this link.

Eclipse exported Runnable JAR not showing images

Community
  • 1
  • 1
Vignesh Shiv
  • 1,129
  • 7
  • 22
0

Because the image is not separate file but packed inside the .jar.

Use the code to create the image from stream

InputStream is=Launcher.class.getResourceAsStream("/1.png");
Image img=ImageIO.read(is);
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • And how do I get from the inputstream to file? In other words how do I see if the file exists? – Bosiwow Jan 13 '15 at 11:40
  • 1
    the stream is not null – StanislavL Jan 13 '15 at 11:44
  • Yeah okay, but I'm using a function that expects the PATH to an image. i guess I can't pass the inputstream... – Bosiwow Jan 13 '15 at 11:45
  • 1
    You just don't have the path. If you need a file create it from the stream, store somewhere and use the newly created file passing the path to your function. I would rather rewrite the function to use stream rathar than file. – StanislavL Jan 13 '15 at 12:52
  • It's a library sikuli, for image comparison. I'm not going to rewrite it. But if I do "this.getClass().getResource("/image").getPath()", I'll get the path right? And I can pass that to the method, or am I missing something? – Bosiwow Jan 13 '15 at 12:59
  • The library have another ways to initialize by URL, image (and I guess by stream) E.g. by URL new ImageTarget(new URL("site/targetImage.png")) or by image BufferedImage bigImage = ImageIO.read(new File("targetImage.png")); BufferedImage smallImage = bigImage.subImage(50,50,100,100); Target target = new ImageTarget(smallImage); Yoou have to investigate. File is not only way – StanislavL Jan 13 '15 at 13:09
  • right now I'm using Pattern target=new Pattern(path); where path=this.getClass().getResource("/1.png").getPath(); It works in eclipse. But I've no idea if it will work when exported since I'm having a problem exporting the sikuli library: http://stackoverflow.com/questions/27880151/exporting-an-app-with-sikuli As far as I can see there's only one constructor: http://doc.sikuli.org/pattern.html – Bosiwow Jan 13 '15 at 13:13
  • At the end of this link : http://sikulix-2014.readthedocs.org/en/latest/faq/030-java-dev.html#how-to-use-sikulix-api-in-your-java-programs-or-java-aware-scripting there's something about using images inside jar, but I've got no idea what they mean... – Bosiwow Jan 13 '15 at 13:17
0

try to use this to get image

InputStream input = getClass().getResourceAsStream("/your image path in jar");
abdotalaat
  • 735
  • 7
  • 10
0

Two Simple steps:

1 - Add the folder ( where the image is ) to Build Path;

2 - Use this:

InputStream url = this.getClass().getResourceAsStream("/load04.gif");
myImageView.setImage(new Image(url));
Michel Fernandes
  • 1,187
  • 9
  • 8