This discussion is archived
9 Replies Latest reply: Jul 28, 2012 6:46 PM by 942744 RSS

Error deploying java applet in mac

942744 Newbie
Currently Being Moderated
I am a beginer to both java and applet development, and trying to learn. One step I would like to work out is how to deploy an applet. Of course I have gone through a few tutorials first but I am still having problems getting my app that simply prints a string to deploy correctly. Basically I started a new java project in eclipse with a class TestAp, code below.


import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;

public class TestAp extends JApplet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JLabel lbl = new JLabel("This is a test ap");
add(lbl);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}


I then created a jar file for this project and saved it in my "documents" directory on my mac. I then created an htm file in "documents", using an applet tag, code below

<html>
     <head><title>Title of page</title></head>
     <body>
          This is my first homepage. <b>This text is bold</b>
     </body>

<applet code = 'TestAp.class',
archive = 'testproject.jar',
width = 300,
height = 300 />

</html>

The result is this error when I try to load the html file in either a chrome or safari web browser

load: class TestAp not found.
java.lang.ClassNotFoundException: TestAp
     at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:252)
     at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Plugin2ClassLoader.java:249)
     at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:179)
     at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:160)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
     at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:690)
     at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3045)
     at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1497)
     at java.lang.Thread.run(Thread.java:680)
Exception: java.lang.ClassNotFoundException: TestAp

I know this is a really stupid question, so thanks so much for your time
  • 1. Re: Error deploying java applet in mac
    baftos Expert
    Currently Being Moderated
    I can see a couple of issues, but they don't seem to cause your problem.
    1. The <applet> tag should be inside the <body>.
    2. Just to be safe, use <applet ...></applet> rather than <applet ... />.
    3. code='TestAp' is the right way, not code='TestAp.class'.
    Now about your actual problem. What are the contents of your jar? Did you put inside all the class files resulting from the compilation of TestAp.java?
  • 2. Re: Error deploying java applet in mac
    gimbal2 Guru
    Currently Being Moderated
    baftos wrote:
    I can see a couple of issues, but they don't seem to cause your problem.
    1. The <applet> tag should be inside the <body>.
    Indeed.
    2. Just to be safe, use <applet ...></applet> rather than <applet ... />.
    That makes no difference at all.
    3. code='TestAp' is the right way, not code='TestAp.class'.
    Not according to the Oracle documentation!

    http://docs.oracle.com/javase/7/docs/technotes/guides/plugin/developer_guide/using_tags.html
    Now about your actual problem. What are the contents of your jar? Did you put inside all the class files resulting from the compilation of TestAp.java?
    That is probably the winning question. Class not found - so the most logical conclusion is that the class is not actually in the jar.
  • 3. Re: Error deploying java applet in mac
    942744 Newbie
    Currently Being Moderated
    Thanks for pointing out that the error is likely in my jar file. This is how I generated it, I right exported my project in eclipse to a jar file. which generates a menu where I check both the .project and .classpath files into the jar. There is then 4 options. Export generated class files and resources. Export output folders for checked projects. Export Java source files and resources. Export refactorings for checked projects. At the bottom menu there are 3 options compress the contents. Add directory entries. Overwrite existing files without warning. Is it possible that I am checking the wrong options.

    I can't seem to open my jar file, because there is no main class (since this is an applet there shouldn't be one right?), so I can't tell whats in my jar file. When running my applet in eclipse, it runs just fine but there is a compilation warning. That is


    Description     Resource     Path     Location     Type
    The serializable class TestAp does not declare a static final serialVersionUID field of type long     TestAp.java     /testproject/src     line 6     Java Problem

    could this be breaking my jar file?
  • 4. Re: Error deploying java applet in mac
    baftos Expert
    Currently Being Moderated
    I can't seem to open my jar file, because there is no main class (since this is an applet there shouldn't be one right?), so I can't tell whats in my jar file. When running my applet in eclipse, it runs just fine but there is a compilation warning. That is


    Description     Resource     Path     Location     Type
    The serializable class TestAp does not declare a static final serialVersionUID field of type long     TestAp.java     /testproject/src     line 6     Java Problem

    could this be breaking my jar file?
    No, don't worry about it right now.
    I am on Windows and not using Eclipse, but regrdless, I strongly recommend you learn how to use the command prompt utilities provided with the JDK.
    There is a jar command that allows you to do jar manipulations. I am not saying not to use the Eclipse way, but if you execute "jar tf MyJar.jar" in a terminal window/shell/command prompt (whatever is the right Mac terminology), it displays the jar contents and you can copy and paste them here for us to see.
  • 5. Re: Error deploying java applet in mac
    gimbal2 Guru
    Currently Being Moderated
    939741 wrote:
    I can't seem to open my jar file
    Its a zip file - use whatever zip utility you like to open it. When you just double click it Java will attempt to run it as an executable jar. And just to note: a simple Google query on jar files would have told you that. Don't ignore such an easy source of information.
    The serializable class TestAp does not declare a static final serialVersionUID field of type long TestAp.java /testproject/src line 6 Java Problem
    could this be breaking my jar file?
    No, although you should just let Eclipse generate one for you so the warning is gone. It will be in the list of quick fixes.

    Get in the habit of fixing all warnings that you can fix. They're there for a reason and indicate a possible problem. Usually Eclipse can do the work for you so its really not much of an effort on your part.
  • 6. Re: Error deploying java applet in mac
    942744 Newbie
    Currently Being Moderated
    thanks a bunch, I don't know how I missed that in my google search. So I now can look in my jar file which contains a scr and bin folder and I see that TestAp.class is in the bin folder and TestAp.java is in the src folder. So I am not sure what the problem is. Both my htm file and my jar file are located in the same directory.
  • 7. Re: Error deploying java applet in mac
    baftos Expert
    Currently Being Moderated
    Your jar should contain TestAp.class and TestAp$1.class in the root. Nothing else is necessary except, perhaps a directory META-INF containing a MANIFEST.MF, which is usually generated by the jar creation tool when the correct procedure is followed.
  • 8. Re: Error deploying java applet in mac
    942744 Newbie
    Currently Being Moderated
    yep TestAp$1.class is also in the bin folder. Any other guesses as to whats causing the problem.
  • 9. Re: Error deploying java applet in mac
    DrClap Expert
    Currently Being Moderated
    939741 wrote:
    yep TestAp$1.class is also in the bin folder. Any other guesses as to whats causing the problem.
    You don't need any guesses at all, let alone any more. You already got the correct answer from baftos. If your class is inside a "src" folder or a "bin" folder in your jar, then you did it wrong.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points