简体   繁体   English

class.getResourceAsStream在用户计算机上返回null,如何重现?

[英]class.getResourceAsStream returns null on user's machine, how to reproduce?

I have an issue reported by an user which I cannot reproduce. 我有一个用户报告的问题,我无法复制。 The issue is error: 问题是错误:

Exception in thread "main" java.lang.NullPointerException: source at java.util.Objects.requireNonNull(Unknown Source) at java.util.Scanner.(Unknown Source) at cz.autoclient.Main.getVersion( Main.java:86 ) at cz.autoclient.Main.( Main.java:76 ) at cz.autoclient.Main.main( Main.java:262 )

In this code: 在此代码中:

   private VersionId VERSION = new VersionId("0.0-error");
   public final VersionId getVersion() {
     if(VERSION.affix.equals("error")) {
       InputStream in = Main.class.getResourceAsStream("/version");
       Scanner sc = new Scanner(in, "UTF-8"); // LINE 86
       VERSION = new VersionId(sc.useDelimiter("\\A").next());
     }
     return VERSION;
   }

What I do here is to parse internal resource which contains version info for the application (including version as variable turned out to be very impractical). 我在这里所做的是解析包含应用程序版本信息的内部资源(包括作为变量的版本,事实证明这是非常不切实际的)。

Version file might look something like this: 版本文件可能看起来像这样:

v3.5-beta

The error seems to be caused by the resource "/version" being unreachable. 该错误似乎是由资源"/version"不可访问引起的。 This error does only seem to happen to the user, so I need help why it even might happen. 该错误似乎只发生在用户身上,因此我需要帮助,甚至可能会发生此错误。 User's Java version is: 用户的Java版本是:

java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

Here is the full Main.java (specific commit) . 这是完整的Main.java (特定提交) The application is distributed as jar file. 该应用程序以jar文件的形式分发。 The version file is in the root of the jar file. version文件位于jar文件的根目录中。

In case it happened to be unclear the problem is when I run the same release (downloaded from release site, not re-built), I do not encounter the problem . 如果碰巧不清楚,问题是当我运行相同的发行版(从发行版站点下载,未重建)时,我不会遇到问题 I asked the user to verify that the file is there (by unziping the jar file). 我要求用户验证文件是否存在(通过解压缩jar文件)。 He sent it to me, it was intact . 他把它寄给我了,它完好无损

Evidently you ran the code in your IDE, which doesn't use the built JAR file at all: it uses the directory hierarchy produced by the compilation step. 显然,您在IDE中运行了代码,它根本不使用构建的JAR文件:它使用了编译步骤生成的目录层次结构。 All you you have to do here is check the content of the JAR file you shipped, which obviously does not contain /version . 您要做的就是检查您附带的JAR文件的内容,该文件显然不包含/version The way to reproduce this problem is to run the same JAR file the same way the customer did, in a clean install. 重现此问题的方法是在全新安装中以与客户相同的方式运行相同的JAR文件。 However that's not what you're really asking. 但是,这并不是您真正要问的。 The solution is to get rid of the resource, and put the version information into the JAR manifest, where you can't forget to ship it. 解决方案摆脱资源,并将版本信息放入JAR清单中,您不能忘记将其发布。

User claims that he uninstalled MacAfee antivirus (distributed with their PC) and replaced it with Avast! 用户声称他卸载了MacAfee防病毒软件(随他们的PC分发),并用Avast!代替了它。 and that this fixed the problem. 并且这解决了问题。 Not exactly a programming answer but I decided to share this in case someone faces the same problem. 不完全是编程上的答案,但是我决定分享一下,以防有人遇到相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 class.getResourceAsStream()始终返回null - class.getResourceAsStream() always returns null Java class.getResourceAsStream()返回不正确的字节 - Java class.getResourceAsStream() returns incorrect byte [Class] .class.getResourceAsStream(...)在某些目录的JAR文件中使用时返回null? - [Class].class.getResourceAsStream(…) returns null when used in JAR files in certain directories? Class.getResourceAsStream()问题 - Class.getResourceAsStream() issue Java .class.getResourceAsStream() - Java .class.getResourceAsStream() class.getResourceAsStream 在 android studio 中返回 null - class.getResourceAsStream gives back null in android studio 对ClassLoader.getResourceAsStream()或Class.getResourceAsStream()进行故障排除 - Troubleshooting ClassLoader.getResourceAsStream() or Class.getResourceAsStream() 在Java中使用Class.getResourceAsStream()的问题 - Problems using Class.getResourceAsStream() in Java Class.getResourceAsStream的解释以及如何在Tomcat中为简单的Java Web-应用程序设置它? - Explanation of Class.getResourceAsStream and how it is set in Tomcat for a simple Java web - app? 如何使用Java方法Class.getResourceAsStream()从jar中读取UTF-8文本文件? - How can I read a UTF-8 text file from a jar using the Java method Class.getResourceAsStream()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM