JavaFX 和外部 JAR
我正在尝试部署 JavaFX 应用程序,但是当我在浏览器中运行它时,它似乎无法在外部 jar 文件中找到类。
java.lang.RuntimeException: java.lang.ExceptionInInitializerError
Caused by: java.lang.ExceptionInInitializerError
at common.bo.property.TextAttributeProp.checkLimit(TextAttributeProp.java:125)
我的 lib 文件夹和类路径中有 common.jar。我还在项目属性中使用不受限制的访问。我也了解这个主题 JavaFx 2.0 应用程序引用外部 jars,但这对我不起作用。 当我从 Netbeans 运行它时,一切正常。但在浏览器中,它不会初始化外部 jar 中的类。请你帮助我好吗。 谢谢
jnlp 文件在这里
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0" xmlns:jfx="http://javafx.com" href="JavaFXSklad2.jnlp">
<information>
<title>JavaFXSklad2</title>
<vendor>xxx</vendor>
<description>Sample JavaFX 2.0 application.</description>
<offline-allowed/>
</information>
<resources os="Windows">
<jfx:javafx-runtime version="2.0+" href="http://javadl.sun.com/webapps/download/GetFile/javafx-latest/windows-i586/javafx2.jnlp"/>
</resources>
<resources>
<j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
<jar href="JavaFXSklad2.jar" size="164987" download="eager" />
<jar href="lib/common.jar" size="965131" download="eager" />
<jar href="lib/ifxjdbc.jar" size="705534" download="eager" />
<jar href="lib/infra.jar" size="588915" download="eager" />
<jar href="lib/jdawt.jar" size="726475" download="eager" />
<jar href="lib/toplink.jar" size="1450414" download="eager" />
<jar href="lib/weblogic.jar" size="55582010" download="eager" />
</resources>
<security>
<all-permissions/>
</security>
<applet-desc width="800" height="600" main-class="com.javafx.main.NoJavaFXFallback" name="JavaFXSklad2" />
<jfx:javafx-desc width="800" height="600" main-class="md.MainMenu" name="JavaFXSklad2" />
<update check="background"/>
</jnlp>
编辑>最后我意识到,问题是由这一行jar中的类中的静态块引起的>
String osName= (String) System.getProperties().get("os.name");
我还尝试使用相同的证书对外部 jar 和 javafx jar 进行签名,但没有成功。
I am trying to deploy JavaFX application, but when I run it in browser, it looks like it cannot find the classes in external jar files.
java.lang.RuntimeException: java.lang.ExceptionInInitializerError
Caused by: java.lang.ExceptionInInitializerError
at common.bo.property.TextAttributeProp.checkLimit(TextAttributeProp.java:125)
I have common.jar in my lib folder and classpath. I also use unrestricted access in project properties. And I also know about this topic JavaFx 2.0 application referencing external jars, but it wont work for me.
Everything works fine when I run it from Netbeans. But in browser it wont initialize classes in external jars. Could you please help me.
Thank you
jnlp file is here
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0" xmlns:jfx="http://javafx.com" href="JavaFXSklad2.jnlp">
<information>
<title>JavaFXSklad2</title>
<vendor>xxx</vendor>
<description>Sample JavaFX 2.0 application.</description>
<offline-allowed/>
</information>
<resources os="Windows">
<jfx:javafx-runtime version="2.0+" href="http://javadl.sun.com/webapps/download/GetFile/javafx-latest/windows-i586/javafx2.jnlp"/>
</resources>
<resources>
<j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
<jar href="JavaFXSklad2.jar" size="164987" download="eager" />
<jar href="lib/common.jar" size="965131" download="eager" />
<jar href="lib/ifxjdbc.jar" size="705534" download="eager" />
<jar href="lib/infra.jar" size="588915" download="eager" />
<jar href="lib/jdawt.jar" size="726475" download="eager" />
<jar href="lib/toplink.jar" size="1450414" download="eager" />
<jar href="lib/weblogic.jar" size="55582010" download="eager" />
</resources>
<security>
<all-permissions/>
</security>
<applet-desc width="800" height="600" main-class="com.javafx.main.NoJavaFXFallback" name="JavaFXSklad2" />
<jfx:javafx-desc width="800" height="600" main-class="md.MainMenu" name="JavaFXSklad2" />
<update check="background"/>
</jnlp>
Edit> Finally I realize, that the problem is caused by static block in class in jar on this line>
String osName= (String) System.getProperties().get("os.name");
I also try to sign external jars and javafx jar with the same cetrificate, but without success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是 NetBeans 7.1,则需要将 NetBeans 更新到最新的夜间版本 http: //bits.netbeans.org/download/trunk/nightly/latest/。
早期版本的 NetBeans 在打包 jar 文件时存在错误 http://netbeans.org/bugzilla/show_bug .cgi?id=205844。
您可以通过解压缩应用程序的 jar 文件并检查其 META-INF/MANIFEST.MF 文件是否包含对外部 jar 文件的引用来查看 NetBeans 生成的包是否存在此错误。
看起来您正在使用的 jar(例如 Toplink、jdbc 和 WebLogic)需要的安全权限高于未签名应用程序的可用权限,因此您还需要按照 Sergey 的建议请求进一步的安全权限并签署您的应用程序。为此,请执行以下操作:
请注意,一旦执行此操作,用户在使用您的应用程序时将收到“信息”和“警报”框,因为该应用程序正在请求比普通网页拥有的更高的权限。
在 Java 中,如果类的静态初始化失败,类加载将无法完成,即使该类可用且位于类路径上。这意味着对该类的第一次引用将生成一个
ExceptionInInitializerError
,随后对该类的引用将生成一个经常误导的ClassNotFoundException
。抛出 ExceptionInInitializerError 表示在计算静态初始值设定项或静态变量的初始值设定项期间发生异常。找到包含引发错误的类文件的 Jar,并且已开始加载该类,但无法完成,因为其静态变量或构造函数中出现错误。
可疑代码为
TextAttributeProp.checkLimit(TextAttributeProp.java:125)
,尽管问题的根本原因可能在其他地方。检查 TextAttributeProp 静态代码正在做什么,并将静态初始化方法包装在 try catch 块中以获取更多调试信息、采取回退操作或忽略静态初始化错误。System.getProperties()
不会在未签名的小程序中返回您想要的属性 - 它只会返回 null。然后,您尝试在静态初始值设定项中取消引用此 null 值,这会生成 NullPointerException 并导致类加载失败,并且随后对该类的引用会出现 ClassNotFoundException。如果您通过
System.getProperty("os.name")
直接获取属性"os.name"
而不是获取所有系统属性,那么它会起作用,因为" os.name"
未标记为安全敏感。要查看可以或不可以从 Applet 请求哪些属性,请参阅:http ://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/properties.html。另请注意,未签名的小程序无法设置任何系统属性。如果您的应用程序已正确签名,您应该能够获取和设置您想要的任何系统属性。如果您无法执行此操作,则可以在 http://javafx-jira.kenai 上提交针对 JavaFX 浏览器插件的错误.com。首先,通过尝试执行其他需要提升权限的操作来检查您是否已正确签署应用程序,例如,从已签名的小程序中读取根文件系统中的文件,并确保读取不会失败。
If you are using NetBeans 7.1, you need to update your NetBeans to the latest nightly version http://bits.netbeans.org/download/trunk/nightly/latest/.
Earlier versions of NetBeans have a bug packaging jar files http://netbeans.org/bugzilla/show_bug.cgi?id=205844.
You can see if your NetBeans generated package has this bug by unjarring your application's jar files and checking that it's META-INF/MANIFEST.MF file includes references to your external jar files.
It looks like the jars you are using (things like Toplink, jdbc and WebLogic) require security privileges above what you are available to an unsigned application, so you also need to request further security permissions as Sergey recommends and sign your application. To do this:
Note that once you do this, users will now get Info and Alert boxes when they use your application because the application is requesting increased privileges over what a normal web page would have.
In Java, if static initialization of a class fails, the class load will fail to complete, even though the class is available and on the class path. This means that the first reference to the class will generate an
ExceptionInInitializerError
and subsequent references to the class will generate, an often misleading,ClassNotFoundException
.ExceptionInInitializerError
is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable. The Jar containing the class file throwing the error was found and loading of the class commenced but could not be completed because errors were experienced in it's static variables or constructor.Suspect code would be
TextAttributeProp.checkLimit(TextAttributeProp.java:125)
, although the root cause of your issue may be elsewhere. Check what the TextAttributeProp static code is doing, and wrap static initialization methods in try catch blocks to get more debugging info, take fallback action or ignore the static initialization error.System.getProperties()
will not return you the properties you want in an unsigned applet - it will just return null. You then try to dereference this null value in a static initializer which generates a NullPointerException and causes your class load to fail and subsequent references to the class to turn up ClassNotFoundException.If you directly get the property
"os.name"
viaSystem.getProperty("os.name")
rather than getting all System properties, it works because"os.name"
is not marked as security sensitive. To see which properties you can and cannot request from an Applet, refer to: http://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/properties.html. Also note that an unsigned applet cannot set any System properties.If your application is properly signed, you should be able to get and set any System properties you want. If you cannot do this, then you can file a bug against the JavaFX browser plugin at http://javafx-jira.kenai.com. First check that you have signed your app correctly by trying to do something else which requires elevated privileges, for example, read a file from your root file system from the signed applet and ensure that the read does not fail.
抱歉各位,我确实有点误导你们了。问题是,为什么我的应用程序在 Netbeans 下工作,而在浏览器中却不能工作,原因是我的 jar 中静态块中的这一行:
它在 Netbeans (windows Xp) 中返回正确的值,但在浏览器中返回 Null。但是当我将代码稍微改变一下时>
它也在浏览器中返回正确的值。所以它与外部 jar 或签署应用程序无关。问题仍然存在,因为外部 jar 是我们下属 java 基础设施的一部分,所以我不能直接更改代码,但这将是另一个主题:)
Sorry guys, I did mislead you a little bit. The problem, why my application works under Netbeans and in browser not was this line in static block in my jar:
which return correct value in Netbeans (windows Xp), but Null in Browser. But when I slightly change the code to this>
it return correct value in browser too. So it has nothing to do with external jars or signing the application. The problem still remains, because that external jars is part of our corporal java infrastructure, so I just cannot change the code directly, but this would be another topic :)
从 jnlp 来看,您在打开
无限制访问
后尚未重新创建 jnlp。尝试运行Clean and Build
或手动将下一个标签添加到您的 jnlp 中:Judging by jnlp you haven't recreate jnlp after switching on
Unrestricted access
. Try to runClean and Build
or add next tag to your jnlp manually:我遇到了类似的情况..我不知道哪种解决方案可以帮助你..所以我将全部列出:
1-确保你使用的jar已签名并且签名有效..你可以使用jarsigner目的..因为如果您使用的罐子过期了..应用程序将崩溃@网络。如果有一个罐子签名并且过期了。只需从 META-INF 中删除所有文件即可。 (顺便说一句,jar是zip文件)
2-我看到你的JNLP ..你需要为应用程序类添加此属性:main =“true”,这样你就会:
告诉应用程序哪个是你的应用程序类很重要..我不知道知道为什么..但是我在JavaFX的早期版本中发现了这个结构,它解决了我的问题..
3-如果您使用的是webPage而不是通用的..所以只需检查JNLP_content序列是否相同..因为每次你向项目添加/删除 jars,序列号也会改变。还要考虑检查 URL .. 我的意思是 HTML 中的这一部分:
希望这会帮助你或任何面临同样问题的人 ..
祝你好运
I faced similar situations .. I dont know exactly which solution is what may help you .. so I will list them all :
1- be sure that the jar you are using are signed and signature is valid .. you can use jarsigner for this purposes .. because if the jars you are using are expired.. app will crash @ web. if there is a jar signed and expired . just remove all file from META-INF. ( jar is zip file btw)
2-I see your JNLP .. you need to add this attribute for the application class: main="true" so your will be :
Its important to tell app which is your application class .. I dont know why .. but I found this structure in previous version of JavaFX and it solved my problem ..
3- if you are using webPage instead of generic one .. so just check that JNLP_content serial is the same .. because each time you add/remove jars to project, the serial will be changed .consider checking URL as well .. I mean this section in HTML:
hope that is gonna help you or anyone facing the same issue ..
Good luck