Results 1 to 11 of 11
  1. #1
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default SOLVED: Embedding JApplet into webpage first time, nothing shows?

    I've been working on a project locally and it's going great, and I figure it's time to start seeing how it runs on the web. The project will have a jar file with all the classes used, and the archive tag will point to my main GUI class. Still not sure if all that will work.

    Anyway I got the jar file on the site and the html page coded, and a box the right size comes up but nothing is displayed. I figure I'd test it with the JApplet java uses when talking about applets in the beginning

    Java Code:
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    import javax.swing.JLabel;
    
    public class HelloWorld 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("Hello World");
                        add(lbl);
                    }
                });
            } catch (Exception e) {
                System.err.println("createGUI didn't complete successfully");
            }
        }
    }
    my web code is

    Java Code:
    <html> 
    
    <head> 
    <title> favorites / bookmark title goes here </title>
    </head>
    
    <body bgcolor="black" text="white">
    
    <APPLET CODE="HelloWorld.class" WIDTH=800 HEIGHT=789>
    </APPLET>
    
    </body>
    </html>
    Here is a link to what's going on. I know the size is huge but this shouldn't affect it from showing.

    Any help is appreciated, this is exciting regardless!

    UPDATE -- Just read here that they are using a JNLP file and the <script> tags to load this onto the page. Is this necessary? Advantageous?
    Last edited by enerj; 03-09-2011 at 12:00 AM. Reason: solution found

  2. #2
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    You are missing the inner class file

    HelloWorld$1.class

    Put that at the same directory.

    It's a good idea to keep the Java Console on during development. The exception thrown says that "HelloWorld$1.class" is missing on the console.
    Last edited by cultclassic; 03-07-2011 at 07:21 PM.

  3. #3
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    Great tip about the Java console, I see this being valuable in the future.

    I am still confused though, why is it looking for

    HelloWorld$1.class

    when in my html file I tell it to look for

    <APPLET CODE="HelloWorld.class" WIDTH=800 HEIGHT=789>

  4. #4
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Classes with $ in the name are "inner classes". The main class looks loads first, and it then loads/runs the inner class. In your case the statement "new Runnable()..." is creating a new class.

    Google "inner class".

  5. #5
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    Ok I will read up on it, but even my JApplets without the "new Runnable()" call won't load. Classes that extend Applet work okay, but I haven't gotten a JApplet working yet.

    Are there any differences between loading these two to the web? I will take out that runnable call and see if I can get it working, but I don't think I'll have much luck.

    Thanks for the help

  6. #6
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Actually your HelloWorld class works fine for me...
    Last edited by cultclassic; 03-08-2011 at 12:40 AM.

  7. #7
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    I see you have the HelloWorld$1.class in your test folder, I just saw that in my bin folder for that project and I think it'll work if I add that. I just tested it with a simple JApplet using JPanels and it works, so thank you.

    Now on to the real issue at hand. Using your console tip, I investigated why my main program that is contained in a jar file won't work. The error from the console is saying it can't find the class "SplineInterpolator" which is an Apache math commons package I've added to my project, and when I look in the jar file that my ARCHIVE tag points to, "commons-math-2.1.jar" is located in /lib and all the class files are in /.

    This is the html call

    Java Code:
    <APPLET ARCHIVE="grapher.jar" CODE=GUImain.class 
    WIDTH=800 HEIGHT=789>
    </APPLET>
    and here is the error from the console.

    Java Code:
    Exception in thread "thread applet-GUImain.class-6" java.lang.NoClassDefFoundError: org/apache/commons/math/analysis/interpolation/SplineInterpolator
    	at Graph.<init>(Graph.java:42)
    	at Graph.<init>(Graph.java:57)
    	at GUIdefiner.<init>(GUIdefiner.java:90)
    	at GUImain.init(GUImain.java:18)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Everything works great offline, it's the web transition I'm having trouble with. I know this is beyond the scope of this thread, but any additional help is much appreciated.

  8. #8
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Include the location of commons-math-2.1.jar into your ARCHIVE or add it in the manifest file of grapher.jar

  9. #9
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    Cult, thanks for single handedly teaching my java jar and applet basics =)

    I've already started reading about manifest files and multiple JARs w/ ARCHIVE. If you've got time, what would a jar inside a jar call look like?

    Java Code:
    <APPLET ARCHIVE="grapher.jar, grapher.jar/lib" 
    
    CODE=GUImain.class WIDTH=800 HEIGHT=789>
    </APPLET>

  10. #10
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    This stack overflow thread is related

    java - Classpath including JAR within a JAR - Stack Overflow

    It's the same situation I'm in, right?

    maybe I can just separate the jars and add them both to the archive parameter.

  11. #11
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    Ok I am considering this problem solved. I left the apache math-commons jar out of my class file jar and just put it in the same folder, and used the following applet call

    Java Code:
    <APPLET ARCHIVE="grapher.jar, commons-math-2.1.jar" CODE=GUImain.class WIDTH=800 HEIGHT=789>

Similar Threads

  1. Embedding Database
    By shalin.shah in forum JDBC
    Replies: 1
    Last Post: 12-12-2010, 07:40 AM
  2. Embedding Jframe displayin a graph in webpage
    By bharath333 in forum Java 2D
    Replies: 1
    Last Post: 04-23-2009, 09:12 PM
  3. Embedding Applet into HTML?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 04:40 AM
  4. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 07:14 AM
  5. Embedding Word in an applet
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •