Search code examples
javajarembedded-resource

Access images inside jar


First. I know that there already exists a variety of topics on how to access the image inside the jar file. Second. I tried many options and none of them did not work. Of course I know that somewhere I make a mistake. Can you help me understand what I'm doing wrong?

so, I have prototype project with name 'j' and it contains just one java class - Client. Client trying to access to image good.png. After I packed all to executable jar file, Client can't accesss file. I work in eclipse, and use ant.

j/
-src/
--com/
---pupcom/
----Client.java
-images/
--good.png
-build.xml
-.classpath
-.project  

com.pupcom.Client contains

package com.pupcom;
//imports;
public class Client {
    public static void main(String [] a) {
        new Client();
    }
    public Client() {
        URL imageURL =  getClass().getClassLoader().getResource("images"+File.separator+"good.png");
        if(imageURL != null){
            Image image = Toolkit.getDefaultToolkit().getImage(imageURL);
            if(image != null){
                System.out.println("Complete!");
            }else{
                System.out.println("image == null");
            }
        }else{
            System.out.println("imageURL == null");
        }
    }
}

build.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project name="j" basedir=".">
    <property name="src.dir" value="src"/>
    <property name="build.dir" value="build"/>
    <property name="main-class" value="com.pupcom.Client"/>
    <property name="jar.name" value="j.jar"/>
    <target name="clean">
        <delete dir="${build.dir}"/>
        <delete file="${jar.name}"/>
    </target>
    <target name="compile" depends="clean">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.dir}/images"/>
        <copy todir="${build.dir}/images">
            <fileset dir="images" />
        </copy>
        <javac srcdir="${src.dir}" destdir="${build.dir}" />
    </target>
    <target name="run" depends="jar">
            <java  jar="${jar.name}" fork="true"/>
        </target>
    <target name="jar" depends="compile">
        <jar destfile="${jar.name}">
            <fileset dir="${build.dir}" />
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>
</project>

I also used these lines:

URL imageURL =  getClass().getClassLoader().getResource(File.separator + "images"+File.separator+"good.PNG");
URL imageURL =  getClass().getClassLoader().getResource("good.PNG");
URL imageURL =  getClass().getResource(File.separator + "images"+File.separator+"good.PNG");
URL imageURL =  getClass().getResource("good.PNG");
URL imageURL =  Client.class.getResource(File.separator + "images"+File.separator+"good.PNG");
URL imageURL =  Client.class.getResource("good.PNG");
URL imageURL =  Client.class.getClassLoader().getResource(File.separator + "images"+File.separator+"good.PNG");
URL imageURL =  Client.class.getClassLoader().getResource("good.PNG");

Thanks for any help!!!!!!!!!!

Thanks to Marko Topolnik, the problem was solved by replacing 'File.separator' to '/'. Many thanks to Marko Topolnik!!!!


Solution

    1. Don't use the File.separator in getResource() - it always takes / (think of it as an URL HREF).
    2. Prefix the path with / to ensure the class loader searches from the root of the class-path, rather than relative to the package of the class.
    3. Check that good.PNG is the correct case. It does not matter on the Windows file-system, but getResource() is case-sensitive.