Exporting jars with resources

前端 未结 3 1695
逝去的感伤
逝去的感伤 2021-01-14 19:43

I\'m trying to build a large Java project by building many small, modular projects. I want each small project to be entirely self contained, holding all of its resources (su

3条回答
  •  时光说笑
    2021-01-14 20:16

    This solution worked for me:

    /**
     *
     * @author David
     */
    public class JavaApplication60 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //http://img.viralpatel.net/java-cup-logo.png
    
        //located in a package not in the current one
        URL url = new JavaApplication60().getResource("/javaapplication60/newpackage/java-cup-logo.png");
        System.out.println(url.getPath());
        //located in same package
        url = new JavaApplication60().getResource("/javaapplication60/java-cup-logo.png");
        System.out.println(url.getPath());
    }
    
    public InputStream getResourceAsStream(String name) {
        name = resolveName(name, this.getClass());
        ClassLoader cl = getClass().getClassLoader();
        if (cl == null) {
            return ClassLoader.getSystemResourceAsStream(name); // A system class.
        }
        return cl.getResourceAsStream(name);
    }
    
    public java.net.URL getResource(String name) {
        name = resolveName(name, this.getClass());
        ClassLoader cl = getClass().getClassLoader();
        if (cl == null) {
            return ClassLoader.getSystemResource(name);  // A system class.
        }
        return cl.getResource(name);
    }
    
    
    /*
     * The resolveName method adds a package name prefix if the name is not
     * absolute, and removes any leading "/" if the name is absolute. It is
     * possible, though uncommon, to have classes in diffent packages sharing
     * the same resource.
     */
    private String resolveName(String name, Object clazz) {
        if (name == null) {
            return name;
        }
        if (!name.startsWith("/")) {
            Class c = clazz.getClass();
            while (c.isArray()) {
                c = c.getComponentType();
            }
            String baseName = c.getName();
            int index = baseName.lastIndexOf('.');
            if (index != -1) {
                name = baseName.substring(0, index).replace('.', '/') + "/" + name;
            }
        } else {
            name = name.substring(1);
        }
        return name;
    }
    }
    

    My package structure looked like this:

    My package structure for above code

    References:

    • Location-Independent Access to Resources

提交回复
热议问题