这是来自http://docs.oracle.com/javase/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html的一个示例我已经剥离了很多功能,因为我只是想让基本的小程序工作但即使我直接从 zip 中运行他们的示例,它给了我一个错误,说 我也尝试将这些类归档到一个 jar 文件中,看看是否有帮助,但它仍然继续给我同样的错误。
我认为问题出在文件夹/文件结构上,但我不确定。
JavaScript 和 HTML
<script>
function enterNums(){
mathApplet.userName = "John Doe";
var greeting = mathApplet.getGreeting();
}
</script>
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {
id:'mathApplet',
code:'jstojava.MathApplet',
width:1,
height:1
} ;
var parameters = {
codebase:"/scripts/java/",
jnlp_href:"math_applet.jnlp"
} ;
deployJava.runApplet(attributes, parameters, '1.7');
</script>
<p><a href="javascript:enterNums();">Launch Example</a></p>
JNLP 文件
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+">
<information>
<title>Math Applet - JavaScript to Java LiveConnect</title>
<vendor>Sun</vendor>
</information>
<resources>
<!-- Application Resources -->
<j2se version="1.6+"
href="http://java.sun.com/products/autodl/j2se"/>
<jar href="jstojava.jar" main="true" />
</resources>
<applet-desc
name="Math Applet"
main-class="MathApplet"
width="1"
height="1">
</applet-desc>
<update check="background"/>
</jnlp>
Java 代码
package jstojava;
import java.applet.Applet;
public class MathApplet extends Applet{
public String userName = null;
public String getGreeting() {
return "Hello " + userName;
}
}