1

我的平台:

  • 视窗;
  • JDK 7;
  • 玻璃鱼3;
  • 记事本++;
  • 命令;

我想部署一个将 Glassfish 归档为 .war 文件的小程序。

java.io当我使用(即ImageIO.read()方法)访问我的资源时,我没有将我的资源归档为 jar 文件,以免自己签署小程序。

这就是我得到的:

ImageViewerApplet.java

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);
    }
}

ImageViewerPanel.java

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);
    }
}

索引.html

<!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>
----------------------------------------------------

我试图解决这个问题两个多月,我的研究没有任何运气。请有人帮我解决这个问题。

4

1 回答 1

2

我没有将我的资源归档为 jar 文件,以免自己签署小程序

Jar 文件不必进行数字签名,它只是一种选择。

OTOH

File BackgroundIMG_File = new File(IMG_Path+"Board.jpg"); 

小程序需要在签名的 Jar 中才能使用File. 如果此图像是小程序的“一部分”,则应由URL.

于 2013-01-13T23:42:18.737 回答