【问题标题】:Accessing resource from a jar shared library从 jar 共享库访问资源
【发布时间】:2015-06-07 15:55:41
【问题描述】:

从 test.java 中,我如何访问以读取位于 contacts.jar 库中的 Contact.xml 文件?

下面的代码返回null。

package views;

import fr.hznteam.contacts.Contact;
import fr.hznteam.errors.ResourceException;

import java.io.InputStream;


    public class Test {

        private static final String TAG = "Test ";

        public static void main(String[] args) throws ResourceException {

            InputStream is = Contact.class.getResourceAsStream("/fr/hznteam/contacts/sql/Contact.xml");
            System.out.println(TAG + "resource from jar " + is);

        }

包结构:

【问题讨论】:

标签: java jar resources


【解决方案1】:

这行得通(见上面的评论)。在对象实例内部使用

getClass().getResourceAsStream("/absolute/class_path/to/resource.file"))

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2016-12-29
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    相关资源
    最近更新 更多