我的平台:
我想部署一个名为Glassfish的applet,归档为.war文件。
我没有将我的资源存档为jar文件,以便在我使用java.io
(即ImageIO.read()
方法)访问我的资源时自行签署applet。
这就是我得到的:
package ImageViewerApplet;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class ImageViewerApplet extends JApplet implements Serializable
{
private Image BackgroundIMG;
private final int APPLET_W = 600;
private final int APPLET_H = 600;
java.net.URL AppletImgURL = ImageViewerApplet.class.getResource("IMG");
private String IMG_Path = AppletImgURL.getPath();
@Override
public void init()
{
Container MyJApplet = getContentPane();
setSize(APPLET_W, APPLET_H);
String IMG_Path = this.IMG_Path+"/";
File BackgroundIMG_File = new File(IMG_Path+"Board.jpg");
try
{
BackgroundIMG = ImageIO.read(BackgroundIMG_File);
}
catch(Throwable ThEx)
{
Logger.getLogger(ImageViewerApplet.class.getName()).
log(Level.SEVERE, null, ThEx);
}
final ImageViewerPanel MyJPanel = new ImageViewerPanel(BackgroundIMG);
MyJApplet.add(MyJPanel);
}
}
package ImageViewerApplet;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ImageViewerPanel extends JPanel
{
private BufferedImage BuffImg = new BufferedImage(600, 600,
BufferedImage.TYPE_INT_RGB);
private Graphics2D Graph = BuffImg.createGraphics();
private Image BackgroundIMG;
public ImageViewerPanel(Image BackgroundIMG)
{
this.BackgroundIMG = BackgroundIMG;
setBackground(Color.pink);
}
@Override
public void paintComponent(Graphics G)
{
Graphics2D Graph2D = (Graphics2D) G;
super.paintComponent(Graph2D);
if(BuffImg == null)
{
System.err.println("BuffImg Is Null");
}
Graph.drawImage(BackgroundIMG, 0, 0, this);
Graph2D.drawImage(BackgroundIMG, 0, 0, this);
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en-US">
<HEAD>
<TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>
<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>
<P>
<APPLET codebase="java" code="ImageViewerApplet.ImageViewerApplet.class" width=600 height=600>
</APPLET>
</P>
</BODY>
</html>
当我使用浏览器以正常方式访问硬盘驱动器上的index.html
时,它可以正常工作并显示背景图像。
然后我这样做:
1) I archive my work on the cmd like this:
-> jar -cvf ImageViewer.war .
2) copy-paste my war file to glassfish "autodeploy" file.
3) after a few seconds this file "ImgViewer.war_deployed" appears,
which means my war file is deployed fine.
使用我的浏览器在本地主机上访问我的工作时出现的问题:
http://localhost:8080/ImgViewer
小程序无效,点击小程序时收到此错误消息:
"RuntimeException java.lang.reflect.InvocationTargetException"
这是我点击细节时得到的结果:
Java Plug-in 10.9.2.05
Using JRE version 1.7.0_09-b05 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Administrator
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
我试图解决这个问题超过两个月,我的研究没有运气。请有人帮我这个。
答案 0 :(得分:2)
我没有将我的资源存档为jar文件,以免我自己签署applet
Jar文件不必进行数字签名,只是一个选项。
OTOH
File BackgroundIMG_File = new File(IMG_Path+"Board.jpg");
小程序需要在签名的Jar中使用File
。如果此图片是applet的“一部分”,则应由URL
访问。