访问jar内的图像 [英] Access images inside jar

查看:127
本文介绍了访问jar内的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先。我知道有关如何访问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?

所以,我有一个名为'j'的原型项目,它只包含一个java类 - 客户端。
客户试图访问image good.png。在我将所有包装到可执行jar文件后,客户端无法访问文件。
我在eclipse工作,并使用ant。

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包含

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:

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>

我也使用过这些行:

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!!!!!!!!!!

感谢Marko Topolnik,将'File.separator'替换为'/'解决了这个问题。
非常感谢Marko Topolnik !!!!

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

推荐答案


  1. 不要使用 File.separator in getResource() - 始终需要 / (将其视为URL HREF)。

  2. 使用 / 前缀路径,以确保类加载器从类路径的根搜索,而不是相对于班级的包裹。

  3. 检查 good.PNG 是否正确。它与Windows文件系统无关,但 getResource()区分大小写。

  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.

这篇关于访问jar内的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆