在JavaWebStart中运行JNLP时出现NullPointerException(jar作为StardArdOne工作)
我写了我的第一个应用程序,但我无法解决一个问题。 使用Eclipse运行应用程序时,一切正常。 导出到runnable jar后(使用将所需库导出到生成的jar中),在Windows中运行jar时,一切正常 当我试图使用jnlp文件和Java Web Start运行应用程序时,它抛出:在JavaWebStart中运行JNLP时出现NullPointerException(jar作为StardArdOne工作),java,jar,java-web-start,embedded-resource,jnlp,Java,Jar,Java Web Start,Embedded Resource,Jnlp,我写了我的第一个应用程序,但我无法解决一个问题。 使用Eclipse运行应用程序时,一切正常。 导出到runnable jar后(使用将所需库导出到生成的jar中),在Windows中运行jar时,一切正常 当我试图使用jnlp文件和Java Web Start运行应用程序时,它抛出: Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException at javax.swing.ImageIcon.<
Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at MedicalOffice.MedicalOfficeView.<init>(MedicalOfficeView.java:122)
抛出NullPointerException
我在我的项目中有一个图像文件夹,将它添加到类路径并使用上面的代码。它只在Eclipse和standardlone jar文件中工作。我检查了jar文件和图像是否存在于主jar文件夹中
这是我的jnlp代码:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0" codbase="http://localhost:8081/MOS/"
href="MedicalOfficeSoftware.jnlp">
<information>
<title>Small Medical Office Management Software</title>
<vendor>Pawel Raczy</vendor>
<description>MedicalOfficeManagement</description>
<offline-allowed/>
</information>
<resources>
<j2se version="1.8+"/>
<jar href="MedicalOfficeSoftware.jar"/>
</resources>
<application-desc main-class="MedicalOffice.MVCMedicalOffice"/>
</jnlp>
除了这个:
URL imgURL = getClass().getClassLoader().getResource("patientsListSmall.jpg");
patientsList.setIcon(new ImageIcon(imgURL));
效果相同-仅适用于eclipse和standardlone jar文件。您的签名jar在清单文件中是否具有Trusted Library属性?“我的项目中有图像文件夹,…”该目录及其内容是否包含在
MedicalOfficeSoftware.jar
?@andreThompson否,将该文件夹添加到类路径后,它被提取到主jar内容(jpg文件直接位于MedicalOfficeSoftware.jar文件中)。我试图使用getClass().getClassLoader().getResource,它直接在类路径中搜索,所以我不明白为什么它会抛出NullPointerException(只有在使用jnlp文件时,jar在没有它的情况下仍能正常工作)。如果我的runnable jar文件也会抛出NullPointerException,我可能知道如何修复它,但它只有在使用jnlp和JavaWebStart时才会抛出此异常。它是否改变了java Mechanical中的任何内容?好的。。请尝试使用“/addPatientSmall.jpg”
”(注意前面的/
)。/
明确告诉JRE查看类路径的“根”。编辑:我刚注意到你提到两个都试。我的建议是只使用前面的/
。在每次尝试中得到的URL
是什么?我觉得您需要使用classLoader.findResource(resourcePath.toExternalForm()
将图像resourcePath
创建到URL。我有一些工作,生产代码,做这件事。这一定是有原因的。创建URL后,它会打印如下内容:文件:/C:/Java/Git/MedicalOfficeManagement软件/SmallMedicalOfficeManagement/bin/addPatientSmall.jpg Hmm getResource()方法默认调用findResource(),而toExternalForm只是从URL返回字符串。我对创建自定义类加载器知之甚少。我得多读一点。然后我会写下结果是什么。
URL imgURL = Thread.currentThread().getContextClassLoader().getResource("patientsListSmall.jpg");
patientsList.setIcon(new ImageIcon(imgURL));
URL imgURL = getClass().getClassLoader().getResource("patientsListSmall.jpg");
patientsList.setIcon(new ImageIcon(imgURL));