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 advance
I 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;
Similar Messages
-
How to include images on a jar file with WSDD
Hello there!
I�m using WSDD 5.7 and I�ve created a build with the "Device Developer Builds" tool to generate a jar file. My project contains a package with images inside, the problem is that everytime I generate the jar file it does not contain that package. Any idea?
cheers!I�m using WSDD 5.7 and I�ve created a build with the
"Device Developer Builds" tool to generate a jar
file. My project contains a package with images
inside, the problem is that everytime I generate the
jar file it does not contain that package. Any idea?I had the same problem and found a workaorund. See my thread in the WSDD forum for details:
http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?message=13774631&cat=9&thread=102493&treeDisplayType=threadmode1&forum=277#13774631 -
How can I access EJB through executable jar file?
Hello buddies!!
I wonder if anyone could give me an explanation of how accessing EJB through executable jar file?Thanks!!Hi there,
You'll need to install the J2EE SDK on the client and include the <j2ee install dir>\lib\j2ee.jar file in your CLASSPATH along with the Client Jar file that was generated by the deploytool when you deployed the application.
In your client code the Context properties are as follows:
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
p.put(Context.PROVIDER_URL, "iiop://hostname:portnumber");
Context cntx = new InitialContext(p).Hope this helps
Amanda
Developer Technical Support
Sun Microsystems
http://www.sun.com/developers -
Dear Friends,
I have a jar file, which has executed classes and methods in it. I want to make use of these methods inside my springcontext piece of code.
Can someone please share an example of how to write spingcontext code which is accessing classes/methods from JAR files along with the any setup?
Thanks,I have found the answer... as described in:
http://java.sun.com/javase/6/docs/technotes/guides/lang/resources.html
the problem was that the properties are loaded with the getResource & getResourceAsStream methods and I didn't know that one. I thought that is was loaded through findClass because I saw the property files trying to be loaded through findClass.
The truth is that it tries to load with the getRessources methods and if it fails tries with the findClass/loadClass.
To Fix the problem, I have simply overriden the getRessourceAsStream to do my magic and that was it.
Thanks -
How can I get a single jar file with NetBeans?
How can I get a single jar file with NetBeans?
When I create the project I get these files:
dist/lib/libreria1.jar
dist/lib/libreria2.jar
dist/software.jar
The libraries that have been imported to create the project are in separate folders:
libreria1/libreria1.jar
libreria2/libreria2.jar
libreria1, libreria2, dist folders are all located inside the project folder.
I added the following code to the build.xml:
<target name="-post-jar">
<jar jarfile="dist/software.jar">
<zipfileset src="${dist.jar}" excludes="META-INF/*" />
<zipfileset src="dist/lib/libreria1.jar" excludes="META-INF/*" />
<zipfileset src="dist/lib/libreria2.jar" excludes="META-INF/*" />
<manifest>
<attribute name="Main-Class" value="pacco.classeprincipale"/>
</manifest>
</jar>
</target>
Of course there is also the project folder:
src/pacco/classeprincipale.form
src/pacco/classeprincipale.java
Can you tell me what is wrong? The error message I get is as follows:
C:...\build.xml:75: Problem creating jar: archive is not a ZIP archive BUILD FAILED (total time: 2 seconds)This is not a NetBeans forum, it is a JDeveloper forum. You might want to try http://forums.netbeans.org/. I also saw your other question - try looking in the New to Java forum: New To Java
-
How to create and use library JAR files with command-line tools?
Development Tools -> General Questions:
I am trying to figure out how to put utility classes into JAR files and then compile and run applications against those JAR files using the command-line javac, jar, and java tools. I am using jdk1.7.0_17 on Debian GNU/Linux 6.0.7.
I have posted a simple example with one utility class, one console application class, and a Makefile:
http://holgerdanske.com/users/dpchrist/java/examples/jar-20130520-2134.tar.gz
Here is a console session:
2013-05-20 21:39:01 dpchrist@desktop ~/sandbox/java/jar
$ cat src/com/example/util/Hello.java
package com.example.util;
public class Hello {
public static void hello(String arg) {
System.out.println("hello, " + arg);
2013-05-20 21:39:12 dpchrist@desktop ~/sandbox/java/jar
$ cat src/com/example/hello/HelloConsole.java
package com.example.hello;
import static com.example.util.Hello.hello;
public class HelloConsole {
public static void main(String [] args) {
hello("world!");
2013-05-20 21:39:21 dpchrist@desktop ~/sandbox/java/jar
$ make
rm -f hello
find . -name '*.class' -delete
javac src/com/example/util/Hello.java
javac -cp src src/com/example/hello/HelloConsole.java
echo "java -cp src com.example.hello.HelloConsole" > hello
chmod +x hello
2013-05-20 21:39:28 dpchrist@desktop ~/sandbox/java/jar
$ ./hello
hello, world!I believe I am looking for:
1. Command-line invocation of "jar" to put the utility class bytecode file (Hello.class) into a JAR?
2. Command-line invocation of "javac" to compile the application (HelloConsole.java) against the JAR file?
3. Command-line invocation of "java" to run the application (HelloConsole.class) against the JAR file?
I already know how t compile the utility class file.
Any suggestions?
TIA,
DavidI finally figured it out:
1. All name spaces must match -- identifiers, packages, file system, JAR contents, etc..
2. Tools must be invoked from specific working directories with specific option arguments, all according to the project name space.
My key discovery was that if the code says
import com.example.util.Hello;then the JAR must contain
com/example/util/Hello.classand I must invoke the compiler and interpreter with an -classpath argument that is the full path to the JAR file
-classpath ext/com/example/util.jarThe code is here:
http://holgerdanske.com/users/dpchrist/java/examples/jar-20130525-1301.tar.gz
Here is a console session that demonstrates building and running the code two ways:
1. Compiling the utility class into bytecode, compiling the application class against the utility bytecode, and running the application bytecode against the utility bytecode.
2. Putting the (previously compiled) utility bytecode into a JAR and running the application bytecode against the JAR. (Note that recompiling the application against the JAR was unnecessary.)
(If you don't know Make, understand that the working directory is reset to the initial working directory prior to each and every command issued by Make):
2013-05-25 14:02:47 dpchrist@desktop ~/sandbox/java/jar
$ cat apps/com/example/hello/Console.java
package com.example.hello;
import com.example.util.Hello;
public class Console {
public static void main(String [] args) {
Hello.hello("world!");
2013-05-25 14:02:55 dpchrist@desktop ~/sandbox/java/jar
$ cat libs/com/example/util/Hello.java
package com.example.util;
public class Hello {
public static void hello(String arg) {
System.out.println("hello, " + arg);
2013-05-25 14:03:03 dpchrist@desktop ~/sandbox/java/jar
$ make
rm -rf bin ext obj
mkdir obj
cd libs; javac -d ../obj com/example/util/Hello.java
mkdir bin
cd apps; javac -d ../bin -cp ../obj com/example/hello/Console.java
cd bin; java -cp .:../obj com.example.hello.Console
hello, world!
mkdir -p ext/com/example
cd obj; jar cvf ../ext/com/example/util.jar com/example/util/Hello.class
added manifest
adding: com/example/util/Hello.class(in = 566) (out= 357)(deflated 36%)
cd bin; java -cp .:../ext/com/example/util.jar com.example.hello.Console
hello, world!
2013-05-25 14:03:11 dpchrist@desktop ~/sandbox/java/jar
$ tree -I CVS .
|-- Makefile
|-- apps
| `-- com
| `-- example
| `-- hello
| `-- Console.java
|-- bin
| `-- com
| `-- example
| `-- hello
| `-- Console.class
|-- ext
| `-- com
| `-- example
| `-- util.jar
|-- libs
| `-- com
| `-- example
| `-- util
| `-- Hello.java
`-- obj
`-- com
`-- example
`-- util
`-- Hello.class
19 directories, 6 filesHTH,
David -
How to handle 2 or more .jar files with an applet
Hey out there
I have created an ftpClient application that uses "jakarta ftpClient". It works fine as an JFrame application � But when I converted the Application into an JApplet I get the following Exception:
java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClient
I have bundled the main application into a .jar file (Application,jar). But I don't know how to handle the 2 jakarta .jar files with my JApplet??
I Tried to append the 2 jakarta .jar files to the Application,jar with the following code:
jar cvf Application.jar 1.class 2.class�. commons-net-1.4.1.jar jakarta-oro-2.0.8.jar
But with the same result / Exception (I have signed the Jar file!)
Can anyone help meHi i have a question with your application can you down- or upload more files at the same time? Because i'm having problems with my ftp application.
Here is the link with my problem maybe you can help me. I will be very pleased when you can help me.
http://forum.java.sun.com/thread.jspa?threadID=5162042&tstart=0
Thx
Satanduvel -
How to install a Application in *.jar file format?
How to install a Application in *.jar file format?
I have taken the *.jar file into the device into media folder. but device is not recognizing the file format
could some one plz provide some suggestion to proceed with this?
Thanks
Mohamed JaveedI'm having the same problem. I've put .jar into the 'system' folder but that doesn't seem to make the program work neverless see it on my device. Help.
-
Read images from a jar file?
Hello, I'm converting a 6i app to 10g. This app does a ton of read_image_file() calls to
change GIF images on the screen based on user actions. In 6i the images are all stored on the local hard drive. The images are not icons for buttons, but images that appear in different locations on the screen.
For 10g, is it possible to leave the images in a jar file and effectively read the images from that? Otherwise, I suppose I'm looking at using webutil functionality to download the jar file and unjar it into an images directory for reading from the client?
Any best-practice scenario for this sort of thing?
Thanks for any info,
GaryWe are migrating from 6i to 10g too. And I try to not use webutil. All my pictures (icons gif files) are in a jar file.
Just make attention of the size of your pictures. The jar file is loaded when you launch the application (the first time, and after, only if the jar file has changed). -
How do I deploy an external JAR file
Hello,
How do I deploy an external JAR file?
I am grateful for every hint.Hi Manuel,
If you read my blog mentioned in the beginning of the thread, you will note that such solution is not supported in our engine due to number of reasons.
Please, read the blog and use application library shared among these two applications. You will achieve the same effect. Please note that the using application will need run and deploy-time dependencies to the shared library.
You should not add files to any of the folders manually or manipulate engine's classpath in such situations.
Best Regards,
Georgi -
How do I store images in a jar file to be displayed on a jsp?
Afternoon all,
I have created a java component which accepts some parameters and returns a string. The returned string contains html tags to display a table in a webpage when rendered through a browser.
I want to be able to add this component to any java web project to display my table. The problem is that I want to use images in my table to give the appearance of curved corners.
How do I store images in my component and render to a webpage?
Thanks in advance,
AlexYou won't be able to make the install of this library as simple as the installation of a jar file. You can't access the images directly in the jar.
There are a few approaches you can take:
1 - jar file + resources:
Have the install as being a jar and a "resources" directory which you store all the images in. Thus the images become part of the web application/web site and are accessible. Probably need to allow an entry in web.xml to configure where/what this directory is called.
2 - jar file only:
Provide a servlet to access the images, which will load them from the jar file, using the ClassLoader.getResourceAsStream(). Requires declaration of a servlet in the web.xml file. Won't be as efficient as having the images on disk, but will keep them bundled in the jar file.
you would generate code such as <img src="myImgServlet?img=.....">
Cheers,
evnafets -
How to access images files outside an ear file
I tried accessing images that are not part of the ear file which i deployed. And
which created on a fly during the execution of applicaiton. I get URL not found
error, can any one let me know if is there any way where in i could access the
files not part of deployed ear. I am uisnig Weblogic 6.1 Awaiting for a reply
ASAP.
Thanks
AnastasiaGenerally, accessing files that are not within your
war hierarchy is not allowed. Everything the webapp
needs should be contained within the webapp.
WebLogic does have an extension known as "virtual
directories" which does allow you to access content
outside the war. There is information on it at:
http://e-docs.bea.com/wls/docs70/ConsoleHelp/war_webappext_virtualdirectorymapping_config.html
mark
> "Ajit Mukka" <[email protected]> wrote in message
> news:[email protected]..
>
> Hi
>
> I tried accessing files that are part of my Default WebApp directory.
> These files are not part of the ear file which i deployed. I create
> few runtime files and make dynamic calls to access those files.
> I get URL not found error, can any one let me know if is there any
> way wherein i could access the files not part of deployed ear.
> awaiting for a reply asap.
>
> Thanks
> Ajit
>
-
How to access images in my localmachine without deploying them
Hi
I am trying to access images in my localmachine from an ADF Application (11.1.2.4). If the source of the images is an url address it works fine. However, if they are in my local machine I don't know how to make it work. I don't want to deploy them with the application. I want them to be referenced like:
file:///C:/images/bla/bla.jpg
I might be failing is something really silly. Apologies in advance.
RegardsYou can only address images inside your web root directly. The application can't just reference your file system.
You can archive this using a servlet which you call via an url with parameters (e.g. the filename) and send the image from there (where you have access to the file system).
Timo -
How does client download Applet dependencies jar files?
I made an Applet which dependencies on several jar files. I think when client browser access this applet, it will download these dependencies. I want to know where these dependencies are located in client machine, will these dependencies are downloaded once. If so, how does the browser handle duplicated downloaded issue.
thanks.It is no business of client code (read 'your applet') to know the location where it is downloaded. The browser will often cache the dependant Jar's, but that is up to the browser and its settings.
For more reliable caching, launch the applet using webstart (either embedded, in 1.6.0_10+ JRE, or free floating). -
How To Load Sound From Inside jar File?
Hello There
i've made a jar file that contains Images&Pictures But I Can't Use Them
So I Used For Loading Images
URL url = this.getClass().getResource("image.jpg");
setIconImage(new ImageIcon(url).getImage());and for loading sound when i use
URL url = this.getClass().getResource("Bond2.wav");
AudioInputStream stream = AudioSystem.getAudioInputStream(new File(url));there's an error That The file Constructor doesn't take url
how can i fix it?First_knight wrote:
and for loading sound when i use
URL url = this.getClass().getResource("Bond2.wav");
AudioInputStream stream = AudioSystem.getAudioInputStream(new File(url));there's an error That The file Constructor doesn't take url
how can i fix it?Remove the "new File()". There is a getAudioInputStream() method that takes a URL.
AudioInputStream in = AudioSystem.getAudioInputStream(url);
Maybe you are looking for
-
"Page cannot be displayed" when opening DatabaseControl in Oracle 11g
Hi, This is a new installation of Oracle 11.2.0.3 on Win 2003 32bit. The install was successful (at least no errors) but I am having a problem with Enterprise Manager. When I go to the web page (https://servername:1158/em) then I receive the error "P
-
Applying a texture to an OrientedShape3D object?
greetings, i am having problems applying a texture to an OrientedShape3D object. i am following the "recipe" and related examples in the j3d tutorial's chapter 7 on textures. my image is a 32x32 gif file that i want to texture onto an OrientedShape3D
-
Flattening an IDOC via Graphical mapping
Hi, I'm still new to the context mapping functions. Though there are numerous examples to do parts of what I would like to do, none seems to address my scenario in such a way that I can combine the mapping methods. My scenario Source IDOC <SEG1>
-
Illustrating with Illustrator and Photoshop cs3
I am just starting out in digital Illustration and was wondering if any one could recomend any books or web sites that discuss using illustrator and photoshop to create illustrations?
-
Transferring files from admin account to standard account w/Filevault on
Hey everybody, Morning! I want to transfer all of my files from my Admin account to a Standard account for better security but Filevault is activated on both. I saw the post in the Apple Support Community: http://discussions.apple.com/thread/30093