How to access files in a jar file?

Can anyone tell me how to access a file contained in my jar file?
I have about 15 classes jarred and deployed as an applet. I want to include in the jar file some .gif files to be used as icons in a tree view. Rather than trying to access them locally with all the security headaches this brings, can I not simply jar them as resources and point to them in my code?
Any help much appreciated,
Thanks in advance,
Chris

Hello Chris,
Please read this note taken directly from Sun, from their Java Tutorial/Creating a GUI with Swing/Using Other Swing Features/How to use Icons/Specifying the Image Source (whew!)
Note:  Applets are supposed to be able to load images from JAR files.
Currently, however, some browsers can't read images from a JAR file,
although they do successfully get classes from a JAR file.
With our applets, we currently hedge our bets by both putting
the image files in the applet's archive file (the JAR file containing
the applet's class files) and by putting the image files in the file
system on the server.
*/So......Do you want to take a chance?
-Ron

Similar Messages

  • 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;

  • 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

  • How can  I access my java class file in a .jar file ...PLz Help anyone!!

    Hi Folks,
    I am new in Java programming, have a .jar file that contains .java files that I want no access and use from another .java file. These files in the .jar file basically form a library thus have no main-method. However I can not declare an instance of the library (in the .jar file) and use it within my program.
    Plz help ...I have tried all I know in vain.
    Thanks.

    temba wrote:
    Hi Folks,
    I am new in Java programming, have a .jar file that contains .java files that I want no access and use from another .java file. These files in the .jar file basically form a library thus have no main-method. However I can not declare an instance of the library (in the .jar file) and use it within my program. You are making little sense. You can't instantiate .java files.
    Plz help ...I have tried all I know in vain.
    Thanks.Could you post WHAT you have tried and also post what error messages you received?

  • Java Files inside the jar file cannot be read or accessed through Eclpse

    The Java File inside the jar file of the eclipse cannot be accessed through the Eclipse . It shows error for the modules in the jar file .
    But when compiled with adding the jar files to the class path the compilation is successful .
    How can i browse through the files in the jar like going into the function definition .
    TIA ,
    Imran

    Open MPlayer OSX and show the playlist (command-shift-p) then drag the file into the playlist. Click the file in the playlist and click the "i"nfo button. What does it list for file, video and audio format?
    Not all of the codecs used in avi and wmv files are available for OS X so I'm guessing your file happens to be using one that isn't...

  • How to use methods from a JAR file inside my springcontext (Oracle fusion 12c) class file?

    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 to load xsl-files out of jar-files

    Hi,
    I have included some xsl-files into a jar-file. I can access this files with the following code-snipped if the jar-file is local on my harddisk:
    ClassLoader.getSystemClassLoader().getResource("pcodeliste.xsl").openStream()
    But the problem is that I use this jar-file as a part of a j2ee-application (it's a ear-file). After deploying this ear-file to my application-server (oc4j) I get a null-pointer exception if I call the xsl-file with the code above.
    Can anybody help me how to change this code or how to change the j2ee-application to have access to this files??
    Thank you very much
    Tobias

    Hi Rykk!
    Thanks for the answer, but it does not work since the jar, containing my file is inside an ear.
    Greets,
    Jay

  • How to read a file inside the JAR file

    Hi All,
    I want to save some preferences in a file called "preferences". I kept the preferences file under my java package.
    If i am running the code inside the NetBeans 6.9 IDE it's working file. But once i have created a JAR and try to run the application, it couldn't find the path.
    Please help me that how to resolve this issue. I don't want to save this preferences file outside of my JAR (i.e) within my java package.
    Here is the code,
    package mypackage;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    public class ReadFile
        private void writeFile()
            try
                OutputStream out = new FileOutputStream(getClass().getClassLoader().getResource("mypackage/preferences").getPath().replace("%20", " "));
                PrintWriter writer = new PrintWriter(out);
                writer.println("Hello Java!");
                writer.close();
                out.close();
            catch (Exception ex)
                System.out.println(ex.getMessage());
        private void readFile()
            try
                FileInputStream fi = new FileInputStream(getClass().getClassLoader().getResource("mypackage/preferences").getPath().replace("%20", " "));
                BufferedReader br = new BufferedReader(new InputStreamReader(fi));
                System.out.println(br.readLine().trim());
                br.close();
                fi.close();
            catch (Exception ex)
                System.out.println(ex.getMessage());
        public static void main(String[] args)
            ReadFile read = new ReadFile();
            read.writeFile();
            read.readFile();
    }If i run the JAR, i get the following error message,
    {color:#ff0000}*file:\my jar path\jarname.jar!\mypackage\preferences (The filename, directoryname, or volume label syntax is incorrect)*{color}

    Thanks sabre150,
    sabre150 wrote:
    You cannot update a running jar file. I am sorry. I didn't know about it. Thanks to point out.
    There are two ways I approach this depending on my exact requirements -
    1) use the java.util.prefs.Preferences APII will try this one
    >
    2) if not already present I copy the preferences file from the jar to a known place. I use a directory in the user's home directory and normally make the directory name the program with a '.' prefix.
    One cannot access files in a jar file using the File API. One needs to use the getResource() or getResourceAsStream() methods on Class or ClassLoader.

  • 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,
    David

    I 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 me

    Hi 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 Javeed

    I'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.

  • How to read a text file from a Jar file

    Hi
    How can I read a text file from a Jar file?
    Thanx in advance..

    thanx
    helloWorld it works.damn, I didn't remove it fast enough. Even if it is urgent, it is best not to mention it, telling people just makes them take longer.

  • How can  I include my database file into my jar file?

    Hi,
    I am doing several tests for the jar command....
    I tried to put the database file into my jar file, but when the program run, it just couldn't find the database file, so how may I include the database file into the jar file? Not only that, it also can't find the policy file etc.

    sorry, I think I found the solution, I should have had read the API more carefully.

  • 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 to create a manifest file to a jar file...

    Hello All,
    I had some problem and I need your help.
    I created one jar file with some of my class files. i want to do my jar file as self executable. How to create a manifest file to a jar file so that it can invoke the main class to start.
    Hope you understand what I want.
    Thank you,
    Rajiv.

    use the following command:
    jar cvfm archive.jar manifest.mf *.*
    (*.* for the class-files)
    the file "manifest.mf" should look like this:
    Manifest-Version: 1.0
    Main-Class: package.MainClass
    where package is your package and MainClass the Classname of your class to start.
    i hope this helps.
    tobias

Maybe you are looking for