我正在尝试使用jnlp运行JApplet。 我创建了MyApplet,它扩展了JApplet并打包在jar中。 我还创建了MyApplet.jnlp和MyApplet.html 我的运行时环境是jdk 1.7.0.02。
当我在浏览器中运行它时,我从浏览器中得到以下异常,但我的applet正在从日食中正常运行
这是我得到的例外
Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$8.run(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDT(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.resize(Unknown Source)
请在下面找到我的代码。这是通过Eclipse运行良好的applet类:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JApplet;
//This is my applet class
public class MyApplet extends JApplet {
private Container Panel;
//constructor
public MyApplet () {
super ();
Panel = getContentPane();
Panel.setBackground (Color.cyan);
}
//paint method
public void paint (Graphics g) {
int Width;
int Height;
super.paint (g);
Width = getWidth();
Height = getHeight();
g.drawString ("The applet width is " + Width + " Pixels", 10, 30);
g.drawString ("The applet height is " + Height + " Pixels", 10, 50);
}
}
这是html文件,MyApplet.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en-US">
<head>
<title>My Applet Menu Chooser Applet</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
</head>
<body>
<noscript>A browser with JavaScript enabled is required for this page to operate properly.</noscript>
<h1>Draggable Menu ChooserApplet</h1>
<p>Press the <b>Left</b> button on your mouse and drag the applet.</p>
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = { code:'MyApplet', width:900, height:300 };
var parameters = {jnlp_href: 'MyApplet.jnlp', draggable: 'true'} ;
deployJava.runApplet(attributes, parameters, '1.6');
</script>
</body>
</html>
MyAppletJnlp.jnlp
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" href="MyApplet.jnlp">
<information>
<title>Jnlp Testing</title>
<vendor>akash sharma</vendor>
<description>Testing Testing</description>
<offline-allowed />
<shortcut online="false">
<desktop />
</shortcut>
</information>
<security>
<all-permissions/>
</security>
<resources>
<!-- Application Resources -->
href="http://java.sun.com/products/autodl/j2se"/>
<jar href="MyApplet.jar" main="true" />
</resources>
<applet-desc
name="Draggable Applet"
main-class="com.acc.MyApplet"
width="900"
height="300">
</applet-desc>
<update check="background"/>
</jnlp>
答案 0 :(得分:1)
我得到了解决方案。实际上在jar文件中,类文件的路径不正确,我在JNLP文件中提到这就是为什么我得到空指针异常。一旦我更新了jar文件我得到它解决了