Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: applets in package

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default applets in package

    I am learning applets and am having a problem (I am sure is very simple for most of you).
    Whenever i put the applet in a namespace (package), i can't run it.
    package javaboard; /*without this it works*/
     
    import java.applet.*;
    import java.awt.*;
     
    public class AppletBoard extends Applet {
    	public void paint(Graphics g){
    		g.drawString("Hello World!",200,100);
    	}
    }
    I tried to create a jar file and modify the class name in the html file but it doesn't work.
    Also before jarring, it didn't work. What am I missing ?

    How do you modify this html
    <html> 
     <applet code="AppletBoard.class" height="300" width="320">
     </applet>
     </html>
    Last edited by dabdi; June 14th, 2011 at 06:39 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: applets in package

    Make sure the directory structure that your code resides parallels the package structure you have created. Further, when you package an app into a jar you need to maintain this structure.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: applets in package

    That also applies to the code= attribute. It should refer to the package and class. For example:
    ... code=javaboard.AppletBoard ...

    Leave off the .class as that is not part of the class name.

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: applets in package

    Quote Originally Posted by Norm View Post
    That also applies to the code= attribute. It should refer to the package and class. For example:
    ... code=javaboard.AppletBoard ...

    Leave off the .class as that is not part of the class name.
    Thanks for your suggestions. I was using the prefix "javaboard." when testing the jars and it worked as long
    as I didnt put the package javaboard; namespace. But I did not try removing .class extension.
    So far I am having no luck both with and without jarring. Here is my directory structure and error message before jarring
     Directory of C:\Users\Daniel\Desktop\test\javaboard
     
    06/14/2011  08:25 PM    <DIR>          .
    06/14/2011  08:25 PM    <DIR>          ..
    06/14/2011  08:25 PM               385 AppletBoard.class
    06/14/2011  08:22 PM                92 AppletBoard.html
    06/14/2011  07:29 PM               191 AppletBoard.java
                   3 File(s)            668 bytes
                   2 Dir(s)  71,459,155,968 bytes free
     
    C:\Users\Daniel\Desktop\test\javaboard>appletviewer AppletBoard.html
    load: class javaboard.AppletBoard not found.
    java.lang.ClassNotFoundException: javaboard.AppletBoard
            at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:211)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
            at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:662)
            at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
            at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
            at sun.applet.AppletPanel.run(AppletPanel.java:368)
            at java.lang.Thread.run(Thread.java:662)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: applets in package

    With the package path, the HTML file should be in the test folder. The browser will look in the javaboard folder for the AppletBoard.class file.

  6. The Following User Says Thank You to Norm For This Useful Post:

    dabdi (June 14th, 2011)

  7. #6
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: applets in package

    Thanks a lot! It works now. So easy when you know ...

Similar Threads

  1. Help With Applets
    By mukilan in forum AWT / Java Swing
    Replies: 12
    Last Post: February 2nd, 2011, 11:18 PM
  2. Drawing in applets. Tell me why this way is wrong.
    By Asido in forum Java Theory & Questions
    Replies: 9
    Last Post: August 1st, 2010, 01:55 PM
  3. Applets
    By santiagomez in forum Java Theory & Questions
    Replies: 2
    Last Post: July 31st, 2010, 04:05 PM
  4. Help needed with applets
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 12th, 2010, 07:36 PM
  5. Learn applets in java
    By mohsendeveloper in forum Java Applets
    Replies: 2
    Last Post: June 25th, 2009, 02:50 PM