问题描述:

Suppose i have the following excerpt of a MANIFEST.MF of an executable jar file:

Manifest-Version: 1.0

Main-Class: com.intersportleitner.skischule.gui.window.SkischulApplicationWindow

Class-Path: .

...

Shouldn't it be possible to have a directory structure like this:

Appdir

|- bla.jar (self-executable)

|- x.properties

|- y.properties

Because if i try to load the properties with the following code-fragment i get a IOException: Stream closed in properties.load(stream):

Properties properties = new Properties();

InputStream istream=SkischulApplicationWindow.class.getClassLoader().getResourceAsStream("y.properties");

BufferedInputStream stream = new BufferedInputStream(istream);

properties.load(stream);

stream.close();

The exception is a bit misleading as actually istream is null (remarked that as i tried to call a method of istream for testing purposes...), so the properties-file isn't found and i have no idea why it fails because according to Executable jar won't find the properties files it should work this way...

网友答案:

getResource and getResourceAsStream are reliant on the class loaders implementation & class path.

I tend to only use this approach when I'm looking for embedded resources, but that's just me

You "could" try

InputStream istream=SkischulApplicationWindow.class.getClassLoader().getResourceAsStream("/y.properties");

Instead

相关阅读:
Top