从JAR内读取文件不能在Windows上工作 [英] Reading file from within JAR not working on windows
问题描述
我试图从一个JAR中打包的文件中读取,以及读取它的类。要做到这一点,我使用以下内容:
$ $ p $ getClass()。getClassLoader()。getResourceAsStream(file)
当我在OSX上创建和运行JAR文件时,这工作正常,但是如果我在Windows上创建并运行JAR文件,上面这一行返回null。
我在这里错过了什么吗?如果我在OSX上创建JAR,并在Windows上运行,它工作正常。这个问题只发生在我在Windows上创建JAR的时候。
编辑:值得一提的是,JAR文件中没有文件夹层次结构。一切都存储在一个层次,因此读取文件的类和文件本身都在同一个目录中。另外,这是我在OSX和Windows上创建JAR文件的方式:
jar -cmf manifest.mf运行.jar * .class file1 file2
编辑2:我要加载的文件是java。属性文件。我认为这不是什么原因造成的问题?
试试这个方法getClass()。getResourceAsStream(/ file1 )。
I'm trying to read from a file that is packaged up inside a JAR, along with the class that reads it. To do this, I use the following:
getClass().getClassLoader().getResourceAsStream(file)
This works fine when I create and run the JAR file on OSX, but if I create and run the JAR file on windows, the above line returns null.
Am I missing something here? If I create the JAR on OSX and run it on Windows it works fine. The problem only occurs when I create the JAR on windows.
EDIT: It's worth mentioning that there is no folder hierarchy within the JAR file. Everything is stored at one level, thus the class reading the file and the file itself are both in the same directory. Additionally, this is how I'm creating the JAR file, on both OSX and Windows:
jar -cmf manifest.mf run.jar *.class file1 file2
EDIT 2: The file I am trying to load is a java .properties file. I take it that's not what is causing the issue?
Try it this way getClass().getResourceAsStream("/file1").
这篇关于从JAR内读取文件不能在Windows上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!