javascript 无法解析 Java 小程序项目中的 JSObject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13953920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:17:29  来源:igfitidea点击:

Not able to resolve JSObject in a java applet project

javajavascriptappletjavafx-2jsobject

提问by deovrat singh

I am trying to call JSObject.getWindow(this)in the initmethod of JAppletbut it is not able to resolve the symbol getWindow.

我正在尝试调用JSObject.getWindow(this)init方法,JApplet但它无法解析符号getWindow

this problem is specifically happening with a javafx application project created through netbeans , getWindow is getting resolved if used in a java application project.

这个问题特别发生在通过 netbeans 创建的 javafx 应用程序项目中,如果在 java 应用程序项目中使用 getWindow 会得到解决。

I have also included the plugin.jarfrom path Java\jdk1.7.0_07\jre\lib.

我还包括了plugin.jarfrom 路径Java\jdk1.7.0_07\jre\lib

This is a javafx application project that I created in netbeans.

这是我在 netbeans 中创建的 javafx 应用程序项目。

回答by deovrat singh

The reason for this is that jfxrt.lib also has a class named JSObject. This JSObject does not have getWindow function defined while plugin.jar one has. If you change the dependency order of JAR and make java to resolve JSObject to plugin.jar then java is able to call getWindow function.

这样做的原因是 jfxrt.lib 也有一个名为 JSObject 的类。这个 JSObject 没有定义 getWindow 函数,而 plugin.jar 有。如果更改 JAR 的依赖顺序并使 java 将 JSObject 解析为 plugin.jar,则 java 可以调用 getWindow 函数。

Following are the links to the javadocs of both the classes.Note that jfxrt one doesn't have getWindow defined.

以下是这两个类的 javadoc 的链接。注意 jfxrt 没有定义 getWindow。

http://docs.oracle.com/javafx/2/api/netscape/javascript/JSObject.html

http://docs.oracle.com/javafx/2/api/netscape/javascript/JSObject.html

http://www.oracle.com/webfolder/technetwork/java/plugin2/liveconnect/jsobject-javadoc/netscape/javascript/JSObject.html

http://www.oracle.com/webfolder/technetwork/java/plugin2/liveconnect/jsobject-javadoc/netscape/javascript/JSObject.html

回答by Steve Bosman

I'm using IntelliJ and just encountered a similar issue.

我正在使用 IntelliJ 并且刚刚遇到了类似的问题。

What I think is happening is that jfxrt.jar and plugin.jar both have netscape.javascript.JSObjectclasses, but the two classes have different signatures. IntelliJ was adding the jars from jre/lib in alphabetical order to the classpath.

我认为正在发生的是 jfxrt.jar 和 plugin.jar 都有netscape.javascript.JSObject类,但是这两个类有不同的签名。IntelliJ 按字母顺序将 jre/lib 中的 jar 添加到类路径中。

By telling IntelliJ not to add jfxrt.jar to the classpath I have been able to resolve my issue because I'm not using JavaFX.

通过告诉 IntelliJ 不要将 jfxrt.jar 添加到类路径,我已经能够解决我的问题,因为我没有使用 JavaFX。

What this points to is that JavaFX requires a different approach to using JSObject - I'm no expert (not even a beginner) at JavaFX but the following page looks useful and suggests to me that calling Javascript is done differently in JavaFX using WebEngine.executeScript(): https://blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx

这表明 JavaFX 需要一种不同的方法来使用 JSObject - 我不是 JavaFX 的专家(甚至不是初学者),但以下页面看起来很有用,并向我建议在 JavaFX 中调用 Javascript 的方式不同WebEngine.executeScript()https: //blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx

回答by tchike

Did you import netscape.javascript.*;?

你有import netscape.javascript.*;吗?

回答by Andrew Thompson

trying to call JSObject.getWindow(this)in the initmethod

尝试调用JSObject.getWindow(this)init方法

The JSObjectis typically not available until start()is called.

JSObject通常不是可用,直到start()被调用。

回答by rajuthoutu

@deorvatsingh Here is the problem not with the netbeans the order you added the jar's. in JavaFx application jfxar.jar is visible first in netbeans cases thats the reason you are not able to get the scope for your JSObject.getWindow(this),

@deorvatsingh 这里的问题不在于您添加 jar 的顺序的 netbeans。在 JavaFx 应用程序中 jfxar.jar 在 netbeans 情况下首先可见,这就是您无法获得范围的原因JSObject.getWindow(this)

For executing this with netbeans and fxappication you first add the the plugin.jar then add jfxrt.jar to your appication then it will recognise.

要使用 netbeans 和 fxappication 执行此操作,您首先添加 plugin.jar,然后将 jfxrt.jar 添加到您的应用程序中,然后它会识别。