Referencing an image in my projects jar file
I don't seem to be able to reference an image in my netbeans project. Here is what it looks like in netbeans:
http://picasaweb.google.com/cendrizzi/JavaStuff/photo#5068570476760118770
Here is my code that I'm using to reference one of these images:
URL imageURL = this.getClass().getResource("images/clock.png");
Image image = Toolkit.getDefaultToolkit().getImage(imageURL);I'm really new at this so I'm not sure what I'm doing wrong. I've tried "images/clock.png" as shown and just "clock.png" . Neither seems to find it there.
Thanks!
Since you provided a relative URL to the getResource method, you need to know that it is relative to the package of the class that called the method. So if your class was in package org.bananarama, you would be looking for the path /org/bananarama/images/clock.png in your jar file.
If you wanted to look for the absolute path /images/clock.png then specify that absolute path.
Similar Messages
-
Images in webforms from jar file
In a headstart generated forms (designer 6 headstart 5.0.3) i have a lot of image items
(unbound items with the hint <IM>my_image.gif</IM>
It's a WebForms application.
I use jar archive file for images. But i see no any image in runtime.
The problem is: in qms$block.init_image_item (qmslib50.pll) is call to read_image_file whit 3 parameters
image_name => 'my_image.gif'
image_type => 'GIF' = substr(l_image_file,instr(l_image_file,'.')+1);
item_name => 'block.my_image_item'
It works on the web too but only if images are in the file system.
If images are in the jar file it don't works.
image_type for the web have to be 'URL' and
image_name my_image (without gif)
What i have to do?
How can i generate form with image_name (without extension) and image_type='URL'?
If it's impossible may be do you have a version of qms$block.init_image_item (for web :-) )
or do i have to change this procedure myself?
Kind regards,
Roman Farber
nullprocedure init_image_item
is
-- purpose: If the hint text contains the name of an image file,
-- enclosed between <IM> and </IM> tags, the image file is
-- is applied to the next item (which is the image item).
-- Procedure is customized for web.
-- Procedure will first looking for an image in archive jar file.
-- If image is not found in jar file we are going to looking for
-- an image in the file syatem.
l_image_file varchar2(50);
l_image_item varchar2(100);
l_image_type varchar2(10);
l_path varchar2(300);
l_path_specified boolean := false;
-- Only for web
l_image_name varchar2(50);
begin
l_image_file := qms$item.get_tagged_hint_info(l_cur_item,'<IM>','</IM>');
if l_image_file is null
then
return;
end if;
-- image file may be stored in forms parameter or global.
-- In both cases the image file should start with a colon
if instr(l_image_file,':') = 1
then
l_image_file := name_in(substr(l_image_file,2));
end if;
l_image_item := l_cur_block| |'.'| |get_item_property(l_cur_item_id,nextitem);
-- check if the next item is really an image item
if get_item_property(l_image_item,item_type) <> 'IMAGE'
then
return;
end if;
-----====================================================
-- For Web Forms, Looking for the image in jar archive file
-- if file is not found (or it's not the application archive jar file )
-- then go to the file system.
SYNCHRONIZE;
-- Name of image without extension.
l_image_name := substr(l_image_file,1,instr(l_image_file,'.')-1);
-- message('l_image_name = '| |l_image_name);pause;
-- Suppress error FRM-47109, cannot locate image file
-- We need to suppress this message because the image files
-- might be located in one of the FORMS50_PATH directories
qms$forms_errors.add_suppress_message('FRM-47109');
read_image_file(l_image_name,'URL',l_image_item);
SYNCHRONIZE;
if not form_success
then
-- message('form not succes');pause;
qms$errors.show_debug_info('Image '| |l_image_file| |
': not found in jar archive file now looking for in file system' );
qms$forms_errors.delete_suppress_message('FRM-47109');
else
-- message('form wel succes');pause;
qms$forms_errors.delete_suppress_message('FRM-47109');
return;
end if;
-- End of looking for the image in jar archive file
-----====================================================
-- file system
l_image_type := substr(l_image_file,instr(l_image_file,'.')+1);
Tool_env.getvar('HEADSTART\IMAGEFILE_PATH', l_path);
if l_path is not null
then
if get_application_property(operating_system) like 'MSWINDOWS%'
or get_application_property(operating_system) = 'WIN32COMMON'
then
if l_path not like ('%\')
then
l_path := l_path| |'\';
end if;
end if;
-- Suppress error FRM-47109, cannot locate image file
-- We need to suppress this message because the image files
-- might be located in one of the FORMS50_PATH directories
qms$forms_errors.add_suppress_message('FRM-47109');
read_image_file(l_path| |l_image_file,l_image_type,l_image_item);
if not form_success
then
qms$errors.show_debug_info('Image '| |l_image_file| |': not found on path '| |l_path
| |', now looking in FORM50_PATH directories' );
qms$forms_errors.delete_suppress_message('FRM-47109');
-- Try to read the image file again, without the path name
-- Forms will automatically search through all directories in the FORMS50_PATH
read_image_file(l_image_file,l_image_type,l_image_item);
qms$errors.show_debug_info('Item '| |l_image_item| |': read image '| |l_image_file);
else
qms$forms_errors.delete_suppress_message('FRM-47109');
qms$errors.show_debug_info('Item '| |l_image_item| |': read image '| |l_path| |l_image_file);
end if;
else
read_image_file(l_image_file,l_image_type,l imageitem);
qms$errors.show_debug_info('Item '| |l_image_item| |': read image '| |l_image_file);
end if;
exception
when form_trigger_failure then raise;
when others then qms$errors.unhandled_exception('qms$block.init_image_item');
end init_image_item; -
Images not appearing in jar file
Hi, i've created an executable jar file for my project in order to send it out for user evaluaution. The problem is, none of the images contained in my project resources package show up when the jar is run (e.g. the start up screen image, GIF/JPG/PNG icons for the fileview of the filechoosers etc..).
I'm basically doing something along the lines of.....
File imgFile = new File(path); //where path is something like "resources/startup.PNG"
imageIcon = new ImageIcon(imgFile.toURL());
to access the images.
I've checked the jar file and the images are definitely in there. Anyone know why they aren't showing up?
thanks
keoBecause your code says that they're files in their own right. You need to either use a jar:// URL or use Class.getResource / Class.getResourceAsStream.
-
How to include images/icons for a jar file
Hi,
I need to create a jar file which will contain my application. My source code (.java) files are in "app" folder. I compiled that using javac -d . *.java and one package("appl") was created. I have all icons and images related to my application are in the folder named "icons" in the directory where source files are located i.e; app folder - "app/icons". I am accessing the images in the icons directory as "icons/book.gif".
I created a jar file from source file directory with the option
jar cvf app.jar app
and i added this jar file to my classpath also.
I have created a batch file to execute this application and set that batch file to "path" environment variable.
It is running from any directory but no icons are appearing in that application if i try to run the application out side the "app" folder.
Can any body give suggessions to solve this problem?Thanks for the added info, now let's get started.
To use files in jars, you need to refer to them via a URL, so you'll need to modify your code a little bit first.
URL imgURL = ClassLoader.getSystemResource("icons/BOOK.gif");
setIconImage(Toolkit.getDefaultToolkit().getImage(imgURL));Next, you'll need a manifest. Using any text editor such as notepad, create a file called manifest.txt and put two lines in it. The first line will specify the class (w/o .class on the end) that contains your public static void main method, the second line is just an empty line (just press return).
Main-Class: main
(a blank line)
Save this file with your class files (e.g. in Exam folder).
Next build your jar. We want the jar to contain the package folder and your icon folder as well. So use this:
jar cmf Exam/manifest.txt myJar.jar Exam/*.class icons
And you should now have an executable jar. You can run it from dos with:
java -jar myJar.jar
or you can double-click it from windows.
-Ron -
How do I load property files and images stored in the jar file?
Hi
So, if I have a structure like this:
/com.myfirm.myproject.SomeClass
/images/
/properties/
And I pack this structure into a jar file, how do I then access the images under "/images/" and the property files under "/properties/"? A link to a good tutorial on this would be perfect, or a description/hint/howto :)Im having problems even loading direcly from the directory..? I have this structure:
src (source dir)
classes (classes dir)
lang/textRb_en_US.properties (the file I want to load)
Then I have this code:
Locale currentLocale = new Locale("en", "US");
ResourceBundle textRb = ResourceBundle.getBundle("/lang/textRb", currentLocale);But it gives me a "java.util.MissingResourceException: Can't find bundle for base name /lang/textRb, locale en_US". I start my application from within Eclipse, and I have tried to move "lang/textRb_en_US.properties" into the classes dir, but same error.
Im confused as to there Java looks for ressources, and I need this to work no matter where I put my classes/jar. Could you help me out here? -
Netbeans 551: Include all libs in project jar file. Possible?
Is it possible to manually, or automaticly, include the /dist/lib directory into my "GonioLabt,jar" to make it more 'stand alone', file wise.
Netbeans generates this dist/README.TXT
========================
BUILD OUTPUT DESCRIPTION
========================
When you build an Java application project that has a main class, the IDE
automatically copies all of the JAR
files on the projects classpath to your projects dist/lib folder. The IDE
also adds each of the JAR files to the Class-Path element in the application
JAR files manifest file (MANIFEST.MF).
To run the project from the command line, go to the dist folder and
type the following:
java -jar "GonioLab.jar"
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Notes:
* If two JAR files on the project classpath have the same name, only the first
JAR file is copied to the lib folder.
* If the classpath contains a folder of classes or resources, none of the
classpath elements are copied to the dist folder.
* If a library on the projects classpath also has a Class-Path element
specified in the manifest,the content of the Class-Path element has to be on
the projects runtime path.
* To set a main class in a standard Java project, right-click the project node
in the Projects window and choose Properties. Then click Run and enter the
class name in the Main Class field. Alternatively, you can manually type the
class name in the manifest Main-Class element.Yes it is possible. The same question has been
killing me all day. Here is how I did it.
(I'm on Mac OS 10.4, but this should work anywhere)
Netbeans produces the following:
dist/myJar.jar
dist/lib/swing-layout-1.0.jar
I would prefer to have only:
dist/myJar.jar
But, as you know, myJar.jar requires the class files
stored in swing-layout-1.0.jar. I unpacked the jar
files and examined the manifest files. Here is how to
unpack and repack the jar files into a single jar
file:
First, I renamed myJar.jar to myJar.zip and let OS X
unarchive the jar for me. I did the same for
swing-layout-1.0.jar.
I then dropped the MANIFEST.MF file from
MyJar/META-INF/ into a new folder on my harddrive
Then, I dropped the package (its a folder full of
class files) from MyJar/ into the same new folder on
my harddrive.
At this point, the new folder contains:
/newFolder/MANIFEST.MF
/newFolder/myPackage/
Then, I grabbed /dist/lib/swing-layout-1.0/org/ and
dropped that into the new folder.
We now have in the new folder:
/newFolder/MANIFEST.MF
/newFolder/myPackage/
/newFolder/org/
Almost done:
Open the MANIFEST.MF file with a text editor. Mine
looked like this:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_07-87 ("Apple Computer, Inc.")
Main-Class: ohm.GUI
Class-Path: lib/swing-layout-1.0.jar
X-COMMENT: Main-Class will be added automatically by
build
I replaced the ENTIRE contents of the file with
this:
Manifest-Version: 1.0
Main-Class: myPackage/Main
Where 'myPackage' is the folder containing my class
files, and 'Main' is the class containing the
'main(String[] args) method.
Save the file and we're ready to repack the archive.
Open the command line (in OS X this is the Terminal)
I assume the syntax is the same in windoze; Navigate
to the new folder you created.
In OSX: cd /newFolder
in windoze: cd \newFolder
Then use java's jar command:
jar cmf MANIFEST.MF MyJar.jar *
What this does:
jar is the command. cmf are flags: the 'c' is for
creating a new jar archive, the 'm' is to specify a
premade manifest file, the 'f' tells jar to write the
results to a file. MyJar.jar is the name you have
chosen for the resulting .jar file. the '*' is a
wildcard character that tells jar to put every file
in the present working directory into the new .jar
Thats it! This produced myJar.jar which worked fine
by itself, as it included all the classes from
swing-layout-1.0.jar. It would be a trivial matter to
write a shell script to do all this for you, unless
of course you use something silly like windows, in
which case you'll have to write a batch file or use
python or something. Good luck, I hope this saves
others time, drop me a e-mail if this was at all
confusing! [email protected]
You should be aware that doing this may well violate the licensing terms for third-party libraries
By the way, what's silly about using Windows? Are you saying you don't develop for Windows because it's "silly"? -
Import a BPM project .jar file in jedevloper
Hi,
Can we import a BPM .jar file in jdeveloper, to regenrate the underlying code. this jar is having the BPM and SOA code.
<< the actual case is to import the deployed BPM .jar file project in jdeveloper. >>
thanks,
rpsYou can unjar the file in your machine so that you have the source code.
Create a new BPM Application (make sure you name the project & process similar to what you have in the JAR).
After that copy & replace the unjared contents into the folder under <applicationName>/<projectName> except for <projectName>.jpr (since this is not in the jar).
try opening it from Jdev .. I could compile using a similar approach and open the BPM process too.
You would need to deploy and test the functionality to be sure that this works. -
Can I see Referenced Albums for Images in a Project in File View?
I am deleting duplicates in a File View in my Projects and I have gone ahead and /highlighted/ the images that are in my Albums with various colors (purple, red, green etc). This was a very helpful suggestion so that I can now rather easily see what Images are definitely being "used" or referenced by another Album.
Am I correct in thinking that there is no way to see this Album in a File View COLUMN or Columns so that I can explicitly know where these Images are being used? Does that question make sense? I mean, is there some widget or plugin (or even some file in the Aperture database that I could open) that would let me see this somehow?
Thanks!for planning services I am assuming that you are using service activity
check if determine plan cost is set up in the network header
secondly make sure that the plan costing variant is set up with the valuation variant to define where to get the plan values from -
How do I display Canvas Graphic image (Not Iconic button) from jar file
Hi all,
I am currently using Forms 10g.
I have gone through the tutorials on how to add Button Icons into Jar files for web enables forms .....so far so good
This however does not explain what I actually want to do... since "graphic images are by nature embeded in the for rather than referenced ......so here goes ..
I have a forms 10g application where all the individual form modules/canvases display a big banner (covering top 20% of the screen). This was done by importing the image straight unto the content canvases. As you know this now becomes a "graphic Image" now embeded(& not referenced by file name) on the canvas. Also bear in mind that this is actually different from an "Image Item" which is a block object.
I dont know if this is possible, but what steps do i follow to make the image part of a jar file as opposed to my current situation. Please let me know if possible or not so I stop waisting development time looking for answers :)
Another question is how do i make this jar file a library object so that we can vary the image as we wish
NB: one other trick which i can think of is to create a giant push button across the screen to display the image and then stick it in a jar file !
Cheers
css_jay99I don't know about embedded images imported in from the builder, but there is a way to use READ_IMAGE_FILE into a BLOCK.IMAGE_ITEM where the images are all in a jar file stored on the OAS.
Metalink Note:137397.1 explains it in details.
Tony -
Problem with images in .jar file
Hi all!
I've got a severe problem when deploying an application via web start:
the images withhin a deployed .jar file are not found.
So far I've tried a number of things to get the images back, none of which are working.
I've found different threads concerning this topic, e.g.:
http://forum.java.sun.com/thread.jspa?threadID=396363
http://forum.java.sun.com/thread.jspa?threadID=465795&messageID=2141351
but sadly, they didn't help me out.
Here is my code I'm using:
private ImageIcon loadIconFromClassLoader(String tszRelPath)
ImageIcon tIcon = null;
cCat.info("tszRelPath = "+ tszRelPath);
// this line simply leads to a crash of the whole application
//tszRelPath = tszRelPath.replaceAll("\\", "/");
if(!tszRelPath.startsWith("/"))
tszRelPath = "/" + tszRelPath;
cCat.info("modified tszRelPath = " + tszRelPath);
URL tURL = ClassLoader.getSystemResource(tszRelPath);
if(tURL == null)
// this too crashes my application
//tURL = PlainResourceProvider.class.getResource(tszRelPath);
if(tURL != null)
tIcon = new ImageIcon(tURL);
return tIcon;
}I've also tried
PlainResourceProvider.class.getClassLoader().getResource()PlainResourceProvider is part of the .jar that involves the images.
Of course all the .class files in the .jar file are easily accesible
I would be so thankful, if just anyone could help me to solve this problem.
I am going crazy.
Thanks in advance, ChristophYou should take care of the directory structure according to the package structure.
I for example have this jar:
C:\source\java\ebank2_util\smsunlock>unzip -l pro-ebank_sms_unlock.jar
Archive: pro-ebank_sms_unlock.jar
Length Date Time Name
0 10-28-05 17:27 hu/
0 10-28-05 17:27 hu/khb/
0 10-28-05 18:17 hu/khb/smsunlock/
256 11-07-05 16:33 hu/khb/smsunlock/DButils$MySQLException.class
4229 11-07-05 16:33 hu/khb/smsunlock/DButils.class
1700 11-07-05 16:33 hu/khb/smsunlock/GUI$1.class
2318 11-07-05 16:33 hu/khb/smsunlock/GUI$2.class
988 11-07-05 16:33 hu/khb/smsunlock/GUI$3.class
1402 11-07-05 16:33 hu/khb/smsunlock/GUI$mywl.class
5630 11-07-05 16:33 hu/khb/smsunlock/GUI.class
818 11-07-05 16:33 hu/khb/smsunlock/LimitedLength_TextField.class
2452 11-07-05 16:33 hu/khb/smsunlock/Main.class
900 11-07-05 16:33 hu/khb/smsunlock/MyInputStream.class
0 11-07-05 16:33 META-INF/
98 10-27-05 15:53 META-INF/manifest.mf
90 11-07-05 16:33 hu/khb/smsunlock/properties
20881 16 filesAnd the resource is loaded like this from hu.khb.smsu.Main:
InputStream is = new MyInputStream( Main.class.getResourceAsStream( "properties" ) ); -
How to access images in a jar file
Hello,
I am currently developping a swing application that uses icons (gif files) and I have zipped all my classes and images in a single .jar file.
When I try to access those files it doesn't work ... it seems that I am not using the right way ...
Could anyone explain me how to achieve this ?
thanks in advanceI get the Images from jar file to set ImageIcon for my components like this:
ImagesLocator.getImage("imageName.gif");
anu
//ImagesLocator.java
import java.net.URL;
import java.awt.*;
import javax.swing.*;
* Load images from correct place (directory or JAR file) into GUI.
public class ImagesLocator
* Load image from correct place (directory or JAR file).
* @param imageName name of image to be loaded (with no path)
* @return loaded image
public static ImageIcon getImage(String imageName)
ClassLoader cl = ImagesLocator.class.getClassLoader();
ImageIcon i = new ImageIcon(cl.getResource(imageName));
return i; -
This took me by surprise:
I assumed that JVM would find image files, e.g. used in (JFrame).setIconImage(img) when these images are inside a jar file with the same path. I am using something like:
Image icon=jframe.getToolkit().getImage(img_path);what works when running JVM with class files and images in directories, but not when in jar file. img_path is relative. Classes will be found as expected, but not the image files!
Do I do something wrong?
Thomas
Edited by: ThomasH_usually on Oct 1, 2008 11:48 AMI encountered this same problem before, whereby I wanted to be able to run my code from within an IDE (find image files from the file system) for development, and also to be able to run my code from within an executable jar (find image files from inside the jar) for release. I used the following utility method to get an image URL:
public static URL getImageUrl(String fileName)
// try to get the URL as a system resource
URL url = ClassLoader.getSystemResource(fileName);
if (url == null)
// try to get the URL directly from the filename
try
url = new URL("file:" + fileName);
catch (Exception e)
return url;
}You can then use the resulting URL instead of the image path, in your case as follows:
Image icon=jframe.getToolkit().getImage(Utils.getImageUrl(img_path));Paul. -
PJC - Loading Images from JAR Files
I'm developing a PJC class that involves loading images. I would like to be able to find and use an image contained in a JAR file just like the built-in Forms items do; not the JAR file where my class is contained but any external ones defined in the server archive settings.
I have seen the loadImage method in the demo RolloverButton class but this only loads images from the class's JAR and not any others.
Is there a VButton method or an Oracle loader class for finding image resources from the defined JAR archives? If not, could someone supply some code for achieving this.
Thank you.Hi,
the jar file name the images are in shouldn't matter for the code as long as the package name is preserved. If you want to see it with your own eyes, do the following
1. Back up demo90.jar
2. Create a copy of it and call it demo90img.jar
3. Open demo90.jar in winzip and remove all images
4. Open demo90img.jar and remove all Java classes
5. add ,/forms90demo/jars/demo90img.jar to the archive_jini tag of a demo you want to test this on
6. Run the application and see the images despite the fact they are now in a separate jar file. Note in the Jinitiator console that the new jar files are downloaded and replace the existing, cached, jars
7. Run an application that doesn't have demo90img.jar configured to see that the images are missing.
Frank -
i use the instruction below to load image contained in my jar file for distribution application but the image doesn't appear
icon = new ImageIcon("images/logo_tunisiana.GIF");
the jar file contain the image under a subdirectory called images.
Witch code sample have i to use ?
thanks in advanceI have an applet that is packaged in a jar file with images. I can access the images using:
aIcon = new ImageIcon(KeyboardApplet.class.getResource("images/keyimages/a.jpg")); -
Read images from external jar file (Sun's Java L&F Graphics repository)
Hi!
I don't know how to read images from an external jar file. Sun has a Java Look and Feel graphics repository that contains a lot of good images that can be used on buttons and so on. This repository is delivered as a jar file, containing only images, and I want to read the image files from that jar file directly without including them inside my application jar file. The application and image jar files will be in the same directory.
I have absolutely no clue how to solve this. Is it necessary to include the images inside my application jar file or is it possible to have them separate as I want.
Would really appreciate some help!
Best regards
LarsHi,
There is two ways :
1) Add your jarfile to the classpath.
Use the class loader :
URL url = ClassLoader.getSystemResource("your/package/image.gif");
Image im = (new ImageIcon(url)).getImage();
or
Image im = Toolkit.getDefaultToolkit().getImage(url);2)If you don't want to add the jar to the classpath you can use this (under jdk 1.4):
import java.util.*;
import java.util.jar.*;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
public class ImageUtilities {
public static Image getImage(String jarFileName, String fileName) {
BufferedImage image = null;
try {
JarFile jar = new JarFile(new File(jarFileName), false, JarFile.OPEN_READ);
JarEntry entry = jar.getJarEntry(fileName);
BufferedInputStream stream = new BufferedInputStream(jar.getInputStream(entry));
image = ImageIO.read(stream) ;
catch (Exception ex) {
System.out.println(ex);
return(image);
}I hope this helps,
Denis
Maybe you are looking for
-
Does my mid 2011 macbook air have turbo boost?
what is turbo boost and does my mac have it? all answers are appreciated thnx guys
-
Switching back to Mail from Outlook (for Mac)
I've had my Mac for only about 8 months now and was happy with Apple Mail - at first. Then I got a hold of MS Outlook 2011 (for Mac) and switched. Now, with the impending revamping of Mail with Mac OS X Lion, I want to switch back to Mail but there d
-
Bad_Pool_Caller - Audigy 2
This is a new thread from an old one...easier to find the problem this way. I am having BSOD with error Bad_Pool_Caller with my Audigy 2 ZS card. I have had this problem ever since installing the new Audigy 2 ZS install replacement disc I got the oth
-
Adding a file into existing zip file
I need a add a file into existing zip file. and write into printwriter. How to do it? Waiting for the reply. Thanks and Regards, Narayanan. G
-
HT204291 Apple TV Air Mirroring
I can't have the mirroring function to work on my new IPAD 3. I have an Apple TV third generation module and a Linksys Router EA3500. When I turn Air Mirroring function ON, my TV screen goes blank? Can Anyone help me?