java.lang.reflect.InvocaciónTargetException

I have an applet with some buttons in it, the buttons have image icons. I also have made an HTML file with this applet. Whenever I open this page from server (Apache tomcat) an exception occurs:

java.lang.reflect.InvocationTargetException.

But if I run without the icons, there are no problems. Can anyone help me, so that I can load the applet with the button icons?

Image myImage = getToolkit().createImage("image/REC1.jpg");

ImageIcon myIcon = new ImageIcon(myImage);
button.setIcon(myIcon);

preguntado el 04 de julio de 12 a las 09:07

Try to use printStackTrace() to know the exact cause of the exception. -

when i run it in netbeans,there is no problem.. the problem only occurs when i run the html file from the server. -

2 Respuestas

Toolkit.createImage(String):

Returns an image which gets pixel data from the specified archivo. The returned Image is a new object which will not be shared with any other caller of this method or its getImage variant.

This method first checks if there is a gerente de seguridad installed. If so, the method calls the security manager's checkRead method with the specified file to ensure that the image creation is allowed.

I made 2 parts bold:

  • Declarar impuestos - only ever points to the local file-system of the computer on which the applet is running. So are unsuitable for icons in the applet - they must be on the server. And..
  • Security Manager - which will not allow a sand-boxed applets to read or write to the local file-system. That might be the important part of the stack trace that is missing.

Applet resource access

Applets need to load resources from the 'home server' if sand-boxed, by URL.

The icons might be an embedded resource, inside a Jar referenced in the archive atributo de la applet element, but if they are loaded to the server as images, they can be accessed relative to the document base or code base. Here is what it might look like for an image named REC1.jpg en la image sub-directory of the directory that contains the HTML.

URL url = new URL(getDocumentBase(), "image/REC1.jpg");
Image myImage = getToolkit().createImage(url);

Respondido el 20 de junio de 20 a las 10:06

Thank you for the help. now the error is gone. but the image is not showing on the button. no error is there. i am really not understanding what is happenning. please help. - Pallab

Seems like the problem in the title of this question has been solved, answered. Why not mark my answer correct (aceptar it) and start a new one? Make sure to show the directory structure of the resources, the HTML used to load the applet, and a short but complete code (SSCCE). - Andrew Thompson

i have posted another question with the code. title: image is notshowing. hope you found it - Pallab

you can use .PNG file format also. and used small letter for file name.

Respondido 04 Jul 12, 09:07

thanks for the reply. but it didnt help much. my problem remains same. - Pallab

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.