getResource()无法在jar中加载内容 [英] getResource() can't load content in a jar

查看:165
本文介绍了getResource()无法在jar中加载内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以成功地加载在Eclipse中的src / dir包中的资源。现在,我导出jar(右键单击src,export,jar并保留默认设置),并且不能在另一个eclipse项目中加载资源。



我构建通过指示一个类位于同一个包名称中的资源路径:

  URL out = getClass()。getClassLoader() .getResource(的packageName(的getClass())+ file.glsl); 
//从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;

你看到错误的方式吗?
我试过:




  • getClass()。getResource()

  • getClass .getClassLoader()。getResource()

  • getClass()。getClassLoader()。getResourceAsStream()

  • Thread.currentThread()。getContextClassLoader() .getResource()

  • 使用'/','\添加/删除程序包名称

  • 的开头处的'/' '或File.separatorChar,甚至'。'都失败了。



我验证了:

$ b $ X-45454545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X-文件名)返回一个干净的名称:\org\jzy3d\plot3d\rendering\ddp\algorithms\dual_peeling_init_vertex.glsl


我无休止地检索一个null:/






编辑:屏幕截图和代码显示应该工作





您可以检索这个一个SSCCE压缩的eclipse项目在这里



用户类

$ b X-4545454545 X-454545 X- 20045 X-454545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X-4545 X- ,shade_vertex.glsl);
System.out.println(found:+ pair.getURL());

}
}

使用新的SomeClass动态提供类( ).getClass()产生相同的结果。



加载器

  package com.pkg.loader; 

import java.io.File;
import java.net.URL; X-45454545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045
}

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; X-45454545 X- 20045 X-4545 X- 200 X- 20045 X- 20045 X- 20045 X- 20045 X-4545 X- 20045 X-4545 X- 20045 X-454545 X-
if(out == null)
throw new RuntimeException(无法找到着色器URL for:'+ getStringPath()+');
退出;
}

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

标记类



<新评新新新新新旗新新新新旗新新旗新新旗200新新新新旗新新旗200新新新新旗200 200 200 200新新新新旗200 200 200新新新新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新div>

首先,尽量不要将源文件直接放在默认包(src)中。
创建至少一个包来包装源代码,因为当jars被导出时,不包括src文件夹。因此,如果您创建自己的默认包,那么您将有一些定义的根目录来基于您的资源路径。



创建自己的默认包后, X-454545451-45 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- /blah/blah/file.glsl);

它适用于我的图片:

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

是的,希望这有帮助。 :)


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

解决方案

First, try not to place source files directly in the default package (src). Create at least one package to wrap your source because when jars are exported, the src folder isn't included. So if you create your own default package, then you'll have some defined root directory to base your resource paths on.

After you've created your own default package, your problem might already be fixed, but if it wasn't then 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"));

Yeah, so, hope this helps. :)

这篇关于getResource()无法在jar中加载内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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