I can successfully load resources that stand in some package of my src/ dir in eclipse. Now, I export the jar (right click src, export, jar, and keep default settings) and can't have the resource loaded in another eclipse project.
I build the path to the resource by indicating a class standing in the same package name:
URL out = getClass().getClassLoader().getResource(packageName(getClass())+"file.glsl"); // out is null when loaded from a jar!! protected static String packageName(Class<?> clazz){ return packageName(clazz.getPackage()); } protected static String packageName(Package p){ return c + p.getName().replace('.', c) + c; } protected static char c = File.separatorChar; Do you see any wrong way? I tried:
- getClass().getResource()
- getClass().getClassLoader().getResource()
- getClass().getClassLoader().getResourceAsStream()
- Thread.currentThread().getContextClassLoader().getResource()
- adding/removing the '/' char at the begining of the package name
- using either '/', '\', or File.separatorChar, and even '.', all failed.
I verified that:
- jar contains the resource (expanding the jar content in eclipse project's "Referenced Libraries")
- packageName(getClass())+filename) returns a clean name: "\org\jzy3d\plot3d\rendering\ddp\algorithms\dual_peeling_init_vertex.glsl"
I endlessly retrieve a null :/
EDIT: screenshot and code to show that "that should work"
You can retrieve this a SSCCE zipped eclipse project here
A user class
public class User { public static void main(String[] args) { Loader pair = new Loader(SomeClass.class, "shade_vertex.glsl"); System.out.println("found:" + pair.getURL()); } } Providing class dynamically with new SomeClass().getClass() produce the same result.
The loader
package com.pkg.loader; import java.io.File; import java.net.URL; public class Loader { public Loader(Class<?> c, String file) { this(packageName(c.getPackage()), file); } public Loader(Package p, String file) { this(packageName(p), file); } protected Loader(String pack, String file) { this.pack = pack; this.file = file; } public String getStringPath() { return pack+file; } public URL getURL() { URL out = Thread.currentThread().getContextClassLoader().getResource(getStringPath()); if(out==null) throw new RuntimeException("unable to find shader URL for :'"+getStringPath()+"'"); return out; } protected static String packageName(Class<?> clazz){ return packageName(clazz.getPackage()); } protected static String packageName(Package p){ return c + p.getName().replace('.', c) + c; } protected static char c = File.separatorChar; protected String pack; protected String file; } The marker class
public class SomeClass {}
System.out.println(packageName(getClass())+"file.glsl"); //debugging 101