简体   繁体   English

Eclipse和可运行的Jar资源

[英]Resources with Eclipse and runnable Jar

I've the following file structure: 我有以下文件结构:

+-src
  +-- code
+-resources
  +-- img
    +- logo.png
  +-- defaultConfig
    +- config.xml

When i run the code in Eclipse it worked, since exporting to runnable jar, it haven't found the defaultConfig files 当我在Eclipse中运行代码时,它可以正常工作,因为导出到可运行的jar文件后,它尚未找到defaultConfig文件

I'm Accessing a Logo this way and it works 我以这种方式访问​​徽标,并且可以正常工作

URL url = getClass().getResource("/img/logo.png");
setIconImage(new ImageIcon(url).getImage());

Accessing the config.xml doesn't work with different set ups. 访问config.xml不适用于不同的设置。

this was given: 给出了:

File config = new File("resources/defaultConfig/config.xml");

after a lot of searching i tried this one: 经过大量搜索后,我尝试了以下操作:

//example
String path = "resources\\defaultConfig\\config.xml");
File config = new File(createURIFromString(path));

I've tried it with ./ & .\\ without . 我已经尝试过使用./ .\\而没有使用.

private URI createURIFromString(String path) {
    URI id = null;
    try {
        id =    getClass().getResource(path).toURI();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return id;
}

Result is a null Pointer. 结果是null指针。

I've tried 我试过了

  • to add the folder to the resource dir 将文件夹添加到资源目录
  • to add the folder to the root dir (same layer like src) 将文件夹添加到根目录(与src相同的层)
  • add the Config folder to the resource dir 将Config文件夹添加到资源目录

Solution: 解:

previously the file weren't in the jar, so till using the getResorce method, it works. 以前该文件不在jar中,所以直到使用getResorce方法,它都可以工作。

Why are you creating a File object? 为什么要创建File对象? Your data is embedded within the jar file; 您的数据嵌入在jar文件中; there's no File object you can construct that refers to it (unless you've got a custom file system somewhere). 没有File它的File对象(除非您在某处有一个自定义文件系统)。

You need to rip out anything which requires the configuration to be read from a file, and instead make it take any InputStream . 您需要删除所有需要从文件读取配置的内容,而使其采用任何InputStream You can then use 然后,您可以使用

InputStream stream = Foo.class.getResourceAsStream("/defaultConfig/config.xml");

This should help you reading xml file inside a jar-package 这应该可以帮助您阅读jar包中的xml文件

"You can't get a File object (since it's no longer a file once it's in the .jar), but you should be able to get it as a stream via getResourceAsStream(path)" “您无法获取File对象(因为一旦它位于.jar中就不再是文件了,但是您应该能够通过getResourceAsStream(path)以流的形式获取它”

If everything is packed, then you should get resources with the getResource the same way you get the images but may be different path. 如果所有内容都打包好了,那么您应该以与获取图像相同的方式使用getResource获取资源,但是路径可能不同。

URL url = getClass().getResource("/defaultConfig/config.xml");

Then you can read directly from this url. 然后,您可以直接从此url阅读。 See Reading Directly from a URL . 请参阅直接从URL读取

 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM