File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Problem Deploying Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Problem Deploying Applet" Watch "Problem Deploying Applet" New topic
Author

Problem Deploying Applet

Jeff Terry
Greenhorn

Joined: Apr 29, 2011
Posts: 7
I have an applet that contains about 30 different class files, all held within two different packages (userInterface and guts), as well as some image files. It also uses an external jar (mysql-connector).

Im having problems viewing it within a browser, though it runs fine in Eclipse. I'm not trying to run it off of a server yet, I'm just putting the project jar file (new.jar) and the html page I create in the same directory (C:/Users/Jeff/Desktop/applet/) on my system, and running it from there.

The class which extends JApplet is in the userInterface package, and is called Main.

Here is my HTML:

<html>
<title>My Applet</title>
<body>
<applet archive='new.jar',
code='userInterface.Main.class',
width=500,
height=500 />
</body>
</html>

I've read through quite a few posts from others experiencing similar issues, but still haven't had any luck after trying to implement their solutions

Thanks for any help you can offer

*EDIT*: I just tried moving 'Main.class' from my jar into a directory (/applet/userInterface/) and changing my html code line to code='userInterface.Main', and am now getting the following errors:

java.lang.RuntimeException: java.lang.ExceptionInInitializerError
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkExit(Unknown Source)
at java.lang.Runtime.exit(Unknown Source)
at java.lang.System.exit(Unknown Source)
at guts.DBHelper.getConnection(DBHelper.java:43)
at userInterface.Main.<clinit>(Main.java:29)
... 21 more
Exception: java.lang.RuntimeException: java.lang.ExceptionInInitializerError

Could this be due to problems connecting to remote db?

I added a java.policy file with:
grant {
permission java.security.AllPermission;
};
to the /applet/ directory, so hopefully this portion of the problem should be fixed.
Jeff Terry
Greenhorn

Joined: Apr 29, 2011
Posts: 7
I think my applet/jar/html's are all in place now, but I can't get past the security errors I am getting when attempting to connect to remote databases.

Reading up on things, I thought the solution may be a crossdomain.xml file placed on the server I was attempting to access. I created one, but now, when I try to run the applet, it just freezes and gives me a black screen (firefox & IE). Chrome still gives errors stating: "Exception: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class userInterface.Main" (so maybe I shouldn't be so confident in how I set up the applet? Still something wrong? The db connection is occurring during Main's init() though)

*EDIT*: Just found a previous post referring to instructions at "http://www-personal.umich.edu/~lsiden/tutorials/signed-applet/signed-applet.html". Checking it out right now.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Does the guts.DBHelper.getConnection method really call System.exit? That wouldn't work in an applet, and is thus not allowed.


Ping & DNS - updated with new look and Ping home screen widget
Jeff Terry
Greenhorn

Joined: Apr 29, 2011
Posts: 7
Yes, it was (which it definitely should not have been). I have removed that line and then got the following error:

java.lang.RuntimeException: java.lang.NoClassDefFoundError: userInterface/ImageComponent
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: userInterface/ImageComponent
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: userInterface.ImageComponent
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 20 more
Exception: java.lang.RuntimeException: java.lang.NoClassDefFoundError: userInterface/ImageComponent

So, after taking the ImageComponent class out of the jar and into the /applet/userInterface directory, I then got errors of other classes not being found. I eventually just extracted everything from the jar straight into the /applet/ directory. Errors are now gone, but the applet just crashes on load and doesn't give a chance for any errors (Firefox: black screen applet, IE: frozen Java loader).

Is this the right way to handle applet file structure? If so, whats the point or the "archive" tag and a jar? Doesn't seem like it was ever using it anyway (though I'm sure my assumptions are incorrect).
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
No, it's not necessary to deploy loose class files - the point of jar files is to have to deal with a single file only. My guess would be that the internal structure of it was somehow incorrect.
Jeff Terry
Greenhorn

Joined: Apr 29, 2011
Posts: 7
Okay, some progress has been made. Following Ulf's suggestions that the structure was wrong, I stopped using the open source Fat Jar to create my jar file, and just used Eclipses regular Export -> Jar File. I wasn't able to get this to also jar up the external mysql-connector jar, so I just copied the mysql jar "com" folder directly into my /applet/ directory.

I was still unable to access the database, until I followed the directions at http://www-personal.umich.edu/~lsiden/tutorials/signed-applet/signed-applet.html to self-sign my applet. Hooray! Now the connection to the remote database is successful.

Now the problem I am running into is reading images into the applet from my jar. I am getting the following error:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at userInterface.ImageComponent.<init>(ImageComponent.java:34)
at userInterface.Main.init(Main.java:100)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.IllegalArgumentException: input == null!

My ImageComponent code:
I was using this code before deployment for reading in images, but I guess its not very applet friendly?
ClassLoader cL = Thread.currentThread().getContextClassLoader();
InputStream iS = ClassLoader.getSystemResourceAsStream(name);
image = ImageIO.read(iS);

So I changed it to:
URL urlResource = getClass().getResource(name);
System.out.println("Got resource...");
image = Toolkit.getDefaultToolkit().getImage(urlResource);
where name is just a String, i.e. "background-1.gif", "background-2.gif", etc.

The part that seems weird to me is, in my Eclipse project all the images are found in the /images/ directory, however, when I export the project to a jar, they are all dumped directly within the top level of the jar (no images directory). Do you think this could be the cause of the problem? Or am I reading them in incorrectly?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Both approaches should work, provided you get the directories sorted out. I'd prefer using ImageIO, though.

It's hard to say what Eclipse does. The only important thing is how the files are layed out in the server directories; that's what matters. IDEs are notorious for not working exactly like an actual server.

I definitely would not keep the images in a jar file, but as loose files.
Jeff Terry
Greenhorn

Joined: Apr 29, 2011
Posts: 7
I put the images in the base /applet/ directory, and then after changing my code to retrieve the images to

//where thisApplet is a static reference to the "Main" applet class itself:
Main.thisApplet.getImage(Main.thisApplet.getDocumentBase(),imageName))

and now its all working!

Thanks for your help Ulf

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem Deploying Applet
 
Similar Threads
Problem Deploying Applet
Applet throwing exception when running it through web browser
Applet to upload files with SCP to
Executable Jar file: Not working after being embedded within a html page?
can not access a member of class docupop with modifiers problem