Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot read a file from META-INF

Tags:

java

I have a big issue doing something really stupid. That is to say open a stream to a resource file in my META-INF folder. I am working on a jar tool and on doing this:

InputStream schemaIS = this.getClass().getClassLoader().getResourceAsStream("/META-INF/schema.xsd");

I simply get a null! The project is built using maven and the xsd file ends up in the META-INF folder, but it still won't work.

What I don't understand is the theory behind it? How do the ClassLoader execute the lookup in the file system? How do I get my hands on the file?

like image 624
nourdine Avatar asked Jan 24 '11 12:01

nourdine


People also ask

How do I get rid of META-INF?

You must open minecraft. jar in a program which can edit . zip files (such as 7-zip), then delete the META-INF folder as you would anything else. This should not require administrator privileges, as minecraft.

What is META-INF file?

The META-INF folder is the home for the MANIFEST. MF file. This file contains meta data about the contents of the JAR. For example, there is an entry called Main-Class that specifies the name of the Java class with the static main() for executable JAR files. Follow this answer to receive notifications.

What is META-INF in APK?

Jar files are used by all types of java applications, they have a specific structure - the META-INF folder contains the manifest information and other metadata about the java package carried by the jar file.

Where do I put META-INF?

It shouldn't be at the project root, but directly under the source folder. At runtime, the persistence. xml file is searched in the classpath, under META-INF. So if you want the META-INF folder to be put at the top of the compiled package tree, you need to put it at the top of the source tree.


2 Answers

Try removing first slash:

InputStream schemaIS = this.getClass().getClassLoader().getResourceAsStream("META-INF/schema.xsd");

You need a leading slash if you are calling the getResourceAsStream method on a Class object. On a ClassLoader object, the path is always absolute, and the leading slash is not necessary.

like image 122
dogbane Avatar answered Sep 22 '22 13:09

dogbane


try this path: META-INF/schema.xsd (first / omitted)

like image 30
wesoly Avatar answered Sep 25 '22 13:09

wesoly