使用扫描程序从打包的 jar 中读取大文件作为资源时的空指针



I'm on Mac OSX 10.12.6

该程序侦听网络上的 Tcp 请求,读取 XML 请求并使用 XML 进行响应。

当我通过命令行运行此程序时:

java -classpath target/classes/com.mycompany.ocip.server.OCIServer

它运行良好并响应所有 XML 请求。

但是,当我将其作为 jar 文件运行时:

Java -jar

target/ocip-server-jar-with-dependencies.jar

我得到一个例外:

java.lang.NullPointerException: source at java.util.Objects.requireNonNull(Objects.java:228) 在java.util.Scanner。(扫描仪.java:578) at com.mycompany.ocip.server.OCIProtocol.readFile(OCIProtocol.java:56) at com.mycompany.ocip.server.OCIProtocol.processInput(OCIProtocol.java:31) at com.mycompany.ocip.server.OCIServer.main(OCIServer.java:85)

当需要发送大型响应时。

异常相关的行位于读取要发送的 XML 文件的代码中:(来自 OCIProtocol.java,如果相关

)
public String readFile(String filename) throws IOException {
InputStream inputStream = this.getClass().getResourceAsStream(filename);
Scanner scanner = new Scanner(inputStream, "UTF-8"); // <-- exception
scanner.useDelimiter("\A");
String text = scanner.hasNext() ? scanner.next() : "";
scanner.close();
return text;
}

奇怪的是这个例外:

  1. 仅在作为 JAR 文件运行时发生
  2. 仅在从类路径读取"大"XML 文件时发生,例如 25MB(几个较小的文件成功)

我已经尝试了几十次。

在将类路径设置为目标//类文件夹的情况下运行程序时,不会发生异常

什么可能导致这种行为?

编辑: 根据要求:焦油输出: 请注意,所有其他XML文件都可以由服务器发送,没有问题,除了examples/ServiceProviderDnGetSummaryListResponse-Client1.xml,(可能相关:此文件为25MB,而其他文件是较小的文件)

user$ tar -tf target/ocip-server-jar-with-dependencies.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/mycompany/
com/mycompany/ocip/
com/mycompany/ocip/server/
examples/
META-INF/maven/
META-INF/maven/bw-ocip-server/
META-INF/maven/bw-ocip-server/bw-ocip-server/
META-INF/maven/com.mycompany/
META-INF/maven/com.mycompany/bw-ocip-server/
com/mycompany/ocip/server/OCIProtocol.class
com/mycompany/ocip/server/OCIServer.class
examples/AuthenticationResponse.xml
examples/GroupDnGetActivationListResponse.xml
examples/LoginResponse.xml
examples/LogoutResponse.xml
examples/ServiceProviderCommunicationBarringCriteriaAssignListRequest.xml
examples/ServiceProviderCommunicationBarringCriteriaAssignListResponse.xml
examples/ServiceProviderCommunicationBarringCriteriaGetAssignedListRequest.xml
examples/ServiceProviderCommunicationBarringCriteriaGetAssignedListResponse.xml
examples/ServiceProviderCommunicationBarringProfileGetResponse-NoSMP.xml
examples/ServiceProviderCommunicationBarringProfileGetResponse.xml
examples/ServiceProviderDnGetSummaryListResponse-Client1.xml
examples/ServiceProviderDnGetSummaryListResponse-NoSP.xml
examples/ServiceProviderDnGetSummaryListResponse.xml
examples/ServiceProviderDnGetSummaryListResponse2.xml
examples/ServiceProviderDnGetSummaryListResponse3.xml
examples/ServiceProviderDnGetSummaryListResponse4.xml
examples/ServiceProviderDnGetSummaryListResponse5.xml
examples/ServiceProviderGetListResponse.xml
examples/SystemCommunicationBarringCriteriaGetListRequest.xml    
examples/SystemCommunicationBarringCriteriaGetListResponse.xml
examples/UserGetListInGroupResponse.xml
META-INF/maven/bw-ocip-server/bw-ocip-server/pom.properties
META-INF/maven/bw-ocip-server/bw-ocip-server/pom.xml
META-INF/maven/com.mycompany/bw-ocip-server/pom.properties
META-INF/maven/com.mycompany/bw-ocip-server/pom.xml

对 readFile 的调用如下所示:

public String processInput(String theInput) throws IOException {
String theOutput = "";
if (theInput.contains("LoginRequest")) {
theOutput = this.readFile("/examples/LoginResponse.xml");
} else if (theInput.contains("LogoutRequest")) {
theOutput = this.readFile("/examples/LogoutResponse.xml");
} else if (theInput.contains("ServiceProviderDnGetSummaryList")) {
theOutput = this.readFile("/examples/ServiceProviderDNGetSummaryListResponse-Client1.xml");
} else if (theInput.contains("UserGetListInGroupRequest")) {
theOutput = this.readFile("/examples/UserGetListInGroupResponse.xml");
} else if (theInput.contains("ServiceProviderCommunicationBarringProfileGetRequest")) {
theOutput = this.readFile("/examples/ServiceProviderCommunicationBarringProfileGetResponse.xml");
} else if (theInput.contains("ServiceProviderCommunicationBarringCriteriaAssignListRequest")) {
theOutput = this.readFile("/examples/ServiceProviderCommunicationBarringCriteriaAssignListResponse.xml");
} else if (theInput.contains("ServiceProviderCommunicationBarringCriteriaGetAssignedListRequest")) {
theOutput = this.readFile("/examples/ServiceProviderCommunicationBarringCriteriaGetAssignedListResponse.xml");
} else if (theInput.contains("SystemCommunicationBarringCriteriaGetListRequest")) {
theOutput = this.readFile("/examples/SystemCommunicationBarringCriteriaGetListResponse.xml");
} else
theOutput = this.readFile("/examples/AuthenticationResponse.xml");
return theOutput;
}

现在您终于提供了足够的详细信息,这里的问题是 JAR 文件中的资源区分大小写,而文件系统显然不是。

该文件存在于 JAR 文件中,并且其名称与文件系统上的名称完全相同

不,不是。资源字符串名称和文件名之间存在大小写差异(DN/Dn)。

最新更新