简体   繁体   English

在Eclipse中可见但在jar中不可见的资源

[英]resources visible in eclipse but not in jar

In a java project I have been given, at one point an external file is referenced. 在一个Java项目中,我曾经引用过一个外部文件。 This file is held in a folder named resources . 该文件保存在名为resources的文件夹中。 In the code, the call is: 在代码中,调用为:

DHKeyReader d = new DHKeyReader("resources/filename")

When run in Eclipse (3.7.2) this executes without failure. 在Eclipse(3.7.2)中运行时,此操作将成功执行。 However, when exported to a jar, this fails with an IOException. 但是,当导出到jar时,此操作将失败,并出现IOException。

I am hesitant to modify the code, as it was given to me under the pretense that it worked and I was to familiarize myself with it so it could be altered. 我很犹豫要修改代码,因为它是在假装有效的情况下提供给我的,我想让自己熟悉它以便可以对其进行更改。 However, there still could be errors. 但是,仍然可能有错误。

My question is this: how do I make a folder (held on the root of the .jar) visible within the jar like it is when running in Eclipse? 我的问题是:如何像在Eclipse中运行时一样,在jar中使文件夹(保存在.jar的根目录中)可见?

This is because at runtime the classloader is not able to find the resource. 这是因为在运行时,类加载器无法找到资源。 Its always better to use : YourClass.class.getResourceAsStream(fileName), if you are using the resources in YourClass. 如果您正在使用YourClass中的资源,则始终最好使用:YourClass.class.getResourceAsStream(fileName)。 This way the resource is searched by the class loader. 这样,类加载器将搜索资源。

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

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