• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loading Images

 
Ranch Hand
Posts: 333
1
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm struggling and need a little help please. My application which I'm running from the Eclipe IDE loads images for icons for buttons using code like this which works fine.

result = new ImageIcon(Common.image_path+filename);

However I ultimately want to package my application into a JAR file and I've tried to replace this code with many variants of this :-

ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("\\Java\\eclipse\\workspace\\b4images"+filename);
result = new ImageIcon(imageURL);

I'm not sure if this would work if the application would run as a .JAR file, but imageURL is always null when I run it as a regular java program.

Is there a way to code it so that it works in either mode. Also I'm unsure about how the path should look. The images are held in a subdirectory called "Images" which is a subdirectory off my main project workspace. However the classloaded is part of a class in a java package, does the path have to be relative to the workspace or the class or neither ?

Thanks

Dave
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that your images are located in the 'b4images' sub-folder of your bin Eclipse project folder, you could do:

ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("/b4images/" + filename);
result = new ImageIcon(imageURL);

Then when it comes to packaging in a jar file, simply put all the images under a sub-folder named 'b4images'.

Remember, either it's under your bin Eclipse project folder, or within your jar file, you should have the same folder structure.

Regards
 
David Garratt
Ranch Hand
Posts: 333
1
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I think I see the problem. My images directory is not a subdirectory of the bin directory. I will have to reorganise my directory structure.

Many thanks

Dave
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope I'm not beating a dead horse, but it's necessary to use URLs, not Files, to identify resources like images so that your program works whether it is zipped in a jar or not. Avoid constructor ImageIcon(String filename), since it assumes the parameter is a file system path. If the image is in the same folder as file X.class (whether that folder is in the file system or a "folder" in a jar file), the following will work:

URL url = X.class.getResource("sample.jpeg");
Icon icon = new ImageIcon(url);

But it's better to separate resources from code, so suppose we have the organization:

C:\blahblah\SampleProject\com\acme\widget\X.class
C:\blahblah\SampleProject\images\sample.jpeg

class X is in package com.acme.widget. When we zip this up in a jar, there should be paths within the jar:

/com/acme/widget/X.class
/images/sample.jar

In either case the code that works will be:

URL url = X.class.getResource("/images/sample.jpeg"); //note initial slash
Icon icon = new ImageIcon(url);

(You can always use forward slashes in URLs.) If you run this on unzipped code the URL expands to

file:/C:/blahblah/SampleProject/images/sample.jpeg

If you run this on the jar file mentioned, the URL might expand to

jar:file:/C:/elsewhere/app.jar!/images/sample.jpeg

So when the code is in a jar, "/images/sample.jpeg" denotes a "path" in the jar file and when the code isn't zipped, "/images/sample.jpeg" extends the folder that contains the start of the package hierarchy. That sounds more complicated than it really is. Once you've set things up (and I prefer using Ant), it's a snap.
 
David Garratt
Ranch Hand
Posts: 333
1
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK this is my abbreviated directory structure which is all of my Eclipse Project workspace c:\java\eclipse\workspace\b4

C:.
+---audio
+---bin
| \---com
| \---commander4j
| +---app
| +---bar
| +---db
| +---sys
| +---util
| \---xtest
+---help
+---images
+---jars
| +---browserlauncher
| +---commons-collections-3.1
| +---commons-digester-1.7
| +---commons-logging-1.0.4
| +---dom4j-1.6.1
| +---itext-1.3
| +---jasperreports-1.0.3
| +---javahelp
| +---JBarcode
| +---logging-log4j-1.2.9
| +---mysql-connector-java-3.1.10
| +---oracle
| +---poi-bin-2.5.1-final-20040804
| \---swt-jasperviewer
+---logs
+---reports
+---src
| \---com
| \---commander4j
| +---app
| +---bar
| +---db
| +---sys
| +---util
| \---xtest
\---xml

The entry point for my application is com.commander4j.sys.Start

and the batch file that runs my application resides in the directory c:\java\eclipse\workspace\b4

%JAVA_HOME%\bin\javaw.exe -Dlog4j.configuration=file:xml\log4j.xml -classpath .\bin;.\jars\blah blah blah...... com.commander4j.sys.Start

So relative to the starting point I have currently referenced my gif files by using a path "./Images/mypic.gif"

Using your syntax then I should be using a url instead of a path and change it to "/Images/mypic.gif"

What is it that determines the root for the URL then ? If my batch file that started the app was it a different directory with a different default directory would with make a difference ?

Thanks muchly,

Dave
 
I've never won anything before. Not even a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic