GetResource () cannot load content into jar

I can successfully load resources that are in some package of my src / dir in eclipse. Now I export the jar (right-click src, export, jar and save the default settings) and cannot load the resource in another eclipse project.

I create a resource path by specifying 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 the wrong way? I tried:

  • GetClass (). GetResource ()
  • GetClass (). GetClassLoader (). GetResource ()
  • GetClass (). GetClassLoader (). GetResourceAsStream ()
  • Thread.currentThread (). GetContextClassLoader (). GetResource ()
  • add / remove '/' char at the beginning of the package name
  • using either '/', '\', or File.separatorChar, and even '.', all failed.

I checked that:

  • jar contains the resource (jar content extension in the eclipse linked libraries project)
  • packageName (getClass ()) + file name) returns the pure name: "\ org \ jzy3d \ plot3d \ rendering \ ddp \ algorithmms \ dual_peeling_init_vertex.glsl"

I infinitely extract zero: /


EDIT: screenshot and code to show that β€œthis should work”

enter image description here

You can get this zpg eclipse project for SSCCE here

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()); } } 

Dynamically create a class using the new SomeClass (). getClass () produces the same result.

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; } 

Marker class

 public class SomeClass {} 
+4
source share
2 answers

First try not to place the source files directly in the default package (src). Create at least one package to wrap your source, because when the containers are exported, the src folder is not included. Therefore, if you create your own default package, then you will have a specific root directory in which your resource paths will be based.

Once you have created your own default package, your problem can already be fixed, but if it is not, try this:

 YourClass.class.getResource("/your_root_package/blah/blah/file.glsl"); 

It works for me for images:

 public static Image ICON32 = Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/files/images/icon32.png")); 

Yes, so hopefully this helps. :)

+2
source

The problem is that this line:

 protected static char c = File.separatorChar; 

This will be a backslash on Windows, but the paths for Class#getResource() must be a slash for it to work. The JRE is probably looking for your path as a relative path and interpreting the backslash literally.

In any case, what you should use for this specific example:

 URL out = getClass().getResource("file.glsl"); 

Class#getResource() already handles the resolution of this relation to the class, so it makes no sense to duplicate this code in your application.

0
source

Source: https://habr.com/ru/post/1397520/


All Articles