简体   繁体   中英

ClassLoader shows wrong path on Linux

I am using classLoader to load xml files located under /src/XMLS :

String m_path = "XMLS/file.xml"

ClassLoader cl = getClass.getClassLoader();
file f1 = new file(cl.getResource(m_path).getFile));

Running on windows it works fine but after export to jar and running it on Linux I get FileNotFoundException - /XMLS/file.xml . I had tried this solutions and I dont think that the problem is in the read from the .jar file. any other ideas for what I am doing wrong?

This will not work for a resource inside a jar file, which is not a file on the filesystem. Instead, you need to use getResourceAsStream() , which returns an InputStream to use directly:

InputStream in = someClass.getClassLoader().getResourceAsStream("/XMLS/file.xml");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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