Cann't get file from classpath (using NIO2)

走远了吗. 提交于 2019-12-25 02:53:57

问题


I want to create a String from the content of the file. According this answer I do it in this way:

private static String buildStringFromTemplate(String stringTemplatePath) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(stringTemplatePath));
    return new String(encoded, "UTF-8");
}

(As I understand this is a path of new NIO2 API, that is a part of Java 7.)

stringTemplatePath parameter is a name of the file ("template.html"). I check location of this file. It is in the classpath: ../classes/template.html

After calling this function I get an exception:

java.nio.file.NoSuchFileException: template.html

Maybe I send filename parameter to in a wrong way? I tried to send this modification: "file:///template.html" and "classpath:template.html", but it didn't help.

Also I tried this code:

private static String buildStringFromTemplate(String stringTemplatePath) throws IOException {
    File file = new File(stringTemplatePath);
    String absolutePath = file.getAbsolutePath();
    byte[] encoded = Files.readAllBytes(Paths.get(absolutePath));
    return new String(encoded, "UTF-8");
}

I called this function I get following exception:

java.nio.file.NoSuchFileException: /opt/repo/versions/8.0.9/temp/template.html

So, file in classpath because new File(stringTemplatePath) can create a File. But this file has very strange path (/opt/repo/versions/8.0.9/temp/template.html). I use Jelastic as hosting (enviroment: Java 8, Tomcat 8), if it is metter.


UPDATE: FINAL WORKING SOLUTION:

private static String buildStringFromTemplate(String stringTemplatePath) throws IOException {
    InputStream inputStream = MyClass.class.getClassLoader().getResourceAsStream(stringTemplatePath);
    return IOUtils.toString(inputStream, "UTF-8"); 
}

IOUtils is util class from Apache IO Commons.

Impotant note:

If I just invoke .getResourceAsStream(...) from class, resource file will be not find and method will return null:

MyClass.class.getResourceAsStream(stringTemplatePath);

So, I call .getClassLoader() before calling .getResourceAsStream(...) and it works perfectly:

MyClass.class.getClassLoader().getResourceAsStream(stringTemplatePath);

回答1:


You should not be trying and accessing resources in your classpath as Paths.

While this will very probably work when your project sits in your IDE setup, it won't as soon as your project is packaged as a jar; it is then impossible to access them using even Path (and even though you can open zip files, therefore jars, as FileSystems).

Use the dedicated methods to do that instead, starting with .getResourceAsStream():

final InputStream in = MyClass.class.getResourceAsStream("/path/to/resource");

Note that you will need to check whether the return code of that method is null (this is what is returned if the resource is not found in the classpath).




回答2:


If the file is really part of the classpath you should use: ClassName.class.getResourceAsStream("/file name") this return InputStraem
or
ClassName.class.getResource("/file name") this return URL



来源:https://stackoverflow.com/questions/29655811/cannt-get-file-from-classpath-using-nio2

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