referencing data files in jars

眉间皱痕 提交于 2019-12-02 10:07:18

I'd recommend you access the files in a classpath-relative way, whether in Eclipse or in a compiled JAR.

There are a few ways you might go about it, but a basic JDK-only approach would be to use Class's getResourceAsStream() method, giving you access to an InputStream object that you can read in.

If your resources are in a jar file, consider using this method to read them:

class Class {
    InputStream getResourceAsStream(String name)
}

This looks for the resource relative to a class (which may be in a jar), rather than relative to the working directory.

Thanks for pointing me down this path, guys. I ended up doing a really hacked up workaround because I'm not very good with IO yet. I used getClass() to construct a URL:

http://forums.sun.com/thread.jspa?threadID=5258488

Then made a new File object from this string (new File(file)):

String file = url.toString().replaceFirst("file:", "");

This allowed me to keep the same code that referenced the file objects.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!