Question

My platform:

  • windows;
  • jdk 7;
  • glassfish3;
  • Notepad++;
  • cmd;

I want to deploy an applet with Glassfish archived as .war file.

I am not archiving my resources as jar file to spare myself signing the applet as I am accessing my resources with java.io(i.e ImageIO.read() method).

This is what I got:

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

index.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>

When I access index.html on my hard-drive the normal way with my browser it works fine and the background image shows up.

Then I do this:

    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.

The problem when I access my work on the local host with my browser like this:

http://localhost:8080/ImgViewer

The applet isn't working and I get this error message when click on the applet:

"RuntimeException java.lang.reflect.InvocationTargetException"

This what I get when I click detail:

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

I am trying to solve this for more than two month, did my research without any luck. Please someone help me with this.

Était-ce utile?

La solution

I am not archiving my resources as jar file to spare myself signing the applet

Jar files do not have to be digitally signed, it is just an option.

OTOH

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

The applet will need to be in a signed Jar to use a File. If this image is 'part of' the applet, it should be accessed by URL.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top