Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么对jar中的资源使用java.lang.ClassLoader.getResource_Java_Classloader - Fatal编程技术网

为什么对jar中的资源使用java.lang.ClassLoader.getResource

为什么对jar中的资源使用java.lang.ClassLoader.getResource,java,classloader,Java,Classloader,使用Java8、Tomcat8+ 我曾经遇到过这样的问题——我试图在lib中读取jar中的txt资源。我使用了以下代码: import java.io.File; import org.apache.commons.io.FileUtils; // .... // code within some class method File file = new File(superService.getClass().getClassLoader()

使用Java8、Tomcat8+

我曾经遇到过这样的问题——我试图在lib中读取jar中的txt资源。我使用了以下代码:

import java.io.File;
import org.apache.commons.io.FileUtils;

// ....    
// code within some class method
File file = new File(superService.getClass().getClassLoader()
                    .getResource("com/domain/example/sorry/confidential/little_text.txt")
                    .getFile())
return FileUtils.readFileToString(file, "UTF-8")
虽然从我的Intellij理念来看,它运行得很好,但在部署到Tomcat服务器后,我遇到了这样的异常:

java.io.FileNotFoundException:文件 '文件:/usr/local/tomcat/webapps/prod lims/WEB-INF/lib/abc-1.2-SNAPSHOT.jar!com/domain/example/sorry/confidential/little_text.txt' 不存在

在那之后,我在SO和其他网站上找到了很多答案,我需要使用ClassLoader.getResourceAsStream()或Class.getResourceAsStream()方法来获取JAR中的资源。当然,我试图用旧式的方式重新设计我的代码——瞧它现在可以工作了:

import java.io.BufferedReader;
import java.io.InputStreamReader;

// ....    
// code within some class method
InputStream inputStream = superService.getClass().getClassLoader()
               .getResourceAsStream("com/domain/example/sorry/confidential/little_text.txt")


StringBuilder resultStringBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))
    try {
        String line;
        while ((line = br.readLine()) != null) {
            resultStringBuilder.append(line).append("\n");
        }
        } catch(Exception e) {
        //just a formal catch - don't mind for now
    }

return resultStringBuilder.toString()

好的,我对有效的解决方案很满意。但我在任何地方都找不到任何明确的解释:为什么getResourceAsStream可以工作,而同时“文件”方法不适用于JAR中的资源?我很好奇,因为
superService.getClass().getClassLoader().getResource(“com/domain/example/sorry/confidential/little_text.txt”)
返回的不是null,但是构造的文件具有
canRead()==false
isExist()==false

文件、FileInputStream、FileUtils等用于访问文件系统,从文件系统的角度来看,嵌入JAR中的“文件”实际上不是文件。因此,您只能通过实际打算从归档文件中读取的工具来访问这些嵌入的文件(基本上,JAR只是一个奇特的ZIP)

顺便说一句,Java中的File对象为非null并不意味着该文件实际存在。你可以做得很好:

File file = new File("I:\\do\\not.exist");
file
将是非空的(但是
isReadable()
isExist()
应该是false)。这与你的资源得到的行为相同


它很可能在IDE中工作,因为该文件在您的工作区中实际上是作为非归档文件存在的。

文件、FileInputStream、FileUtils等用于访问文件系统,而从文件系统的角度来看,嵌入JAR中的“文件”实际上不是文件。因此,您只能通过实际打算从归档文件中读取的工具来访问这些嵌入的文件(基本上,JAR只是一个奇特的ZIP)

顺便说一句,Java中的File对象为非null并不意味着该文件实际存在。你可以做得很好:

File file = new File("I:\\do\\not.exist");
file
将是非空的(但是
isReadable()
isExist()
应该是false)。这与你的资源得到的行为相同

它很可能在IDE中工作,因为该文件在您的工作区中实际上是作为非归档文件存在的。

Sidenote:,开发人员应该使用
java.nio.Path
。Sidenote:,开发人员应该使用
java.nio.Path