开发者

Resources access problem from JAR

开发者 https://www.devze.com 2022-12-23 16:15 出处:网络
I have the following directory structure of a project: Folder \"project\" in Eclipse: --folder \"src\" --folder \"resources\"

I have the following directory structure of a project:

Folder "project" in Eclipse:
--folder "src"
--folder "resources"
----trayicon2.png
--folder "db" 
----test.db
--folder "bin" 

I'm accessing the image with:

Image image = Toolkit.getDefaultToolkit().getImage("resources/trayicon2.png");

开发者_如何学Python

and from Eclipse that is not a problem. Then I generate an "executable jar file", and add the dirs, making a directory structure of:

Folder "project"
--folder "db"
----test.db
--folder "resources"
----trayicon2.png
--project.jar 

And now the image is no more accessible. Also, the database is no more accessible; while in Eclipse I used to access it with:

Connection conn = DriverManager.getConnection("jdbc:sqlite:db/test.db");

How can I access the resources (images and db) after generating the jar file "project.jar"?


For the image, try this:

URL imageURL = getClass().getResource("/resource/trayicon2.png"); // note leading slash
Image image = Toolkit.getDefaultToolkit().getImage(imageURL);


When you are in eclipse the filename you pass is relative and it is resolved properly with the classpath that is automatically setup by eclipse. Try using the URL version of the method and pass the URL to your jar file. You can see an example on this page


You may want to switch to ANT to build your jar files.

You can have the image resources built into the jar where they can be found, and any required libraries (like those for sqlite, put into a lib folder inside the jar as well).

Here's a sample build file:

<?xml version="1.0"?>
<project name="test" default="compile" basedir=".">
    <description>
            Generates jar for project
 </description>
 <property name="src.dir" value="${basedir}/src"/>
    <property name="build.dir" value="${basedir}/build"/>
    <property name="build.classes" value="${build.dir}/classes"/>
    <property name="build.jars" value="${build.dir}/jars"/>
    <target name="init">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.classes}"/>
        <mkdir dir="${build.jars}"/>
    </target>
    <path id="core.classpath">
        <fileset dir="${build.jars}">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <path id='lib.classpath'>
        <fileset dir='lib' />
    </path>

    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${build.classes}" debug="true">
            <classpath refid="lib.classpath"/>
        </javac>
    </target>

 <target name="copy-resources">
        <copy todir="${build.classes}">
            <fileset dir="${src.dir}">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>

    <target name="jar" depends="compile, copy-resources">
        <jar destfile="${build.jars}/project.jar">
            <fileset dir="${build.classes}">
                <include name="**/*.class"/>
                <include name="**/*.properties"/>
                <include name="**/*.png"/>
            </fileset>
         <fileset dir=".">
          <include name="**/*.jar"/>
         </fileset>
         <manifest>
                <attribute name="Built-By" value="${user.name}"/>
             <attribute name="Main-Class" value="com.your.path.to.MainClass"/>
            </manifest>
        </jar>
    </target>

    <target name="clean">
        <delete dir="${build.dir}"/>
        <delete dir="${build.classes}"/>
        <delete dir="${build.jars}"/>
    </target>
</project>

This assumes you have some java files in a src directory, and some libraries in a lib folder. So your path would look like this:

Project
----------
-bin
-src
--com...
---MainClass (has a main method, runs your program)
---a.png (will be findable)
-lib
--jar files required by your project
build.xml
-----------

It will also copy any properties or png files you have inside the src folders.

You would then type ANT jar at the level of build.xml to build the jar file.


You can't access resources inside a JAR file directly. Instead, you need to use the Class's URL getResource(String) (or InputStream getReourceAsStream(String)) methods.

I'm not really sure how you'd use that with the SQLite database, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号