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 7 of 7

Thread: Need help to run my first applet

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help to run my first applet

    I have begun learning about how to make an applet. I have written the code for the applet in eclipse and after i try to put it into a html code and run it . As i see, eclipse may run the applet. The code of the applet is this:
    package mainPack;
     
    import java.applet.*;
    import java.awt.*;
     
    public class FirstApplet extends Applet
    {
    	Image img;
    	public void init()
    	{
    		img = getImage(getCodeBase(), "tom.gif");
     
    	}
    	public void paint(Graphics g)
    	{
    		g.drawImage(img, 50, 50, this);
    		g.drawOval(100, 0, 150, 50);
    		g.drawString("Ce faci FOCA?", 110, 25);
     
    	}
     
    }

    The first problem i have is that the applet does not want to draw the image. I can see the oval and the string but not the image.

    And after i written a notepad with this code :

    <html>
    <head>
    <title>Primul applet Java</title>
    </head>
    <body>
    <applet code=C:\Users\npiulitza\Desktop\Folosire applet\FirstApplet.class width=400 height=400>
    </applet>
    </body>
    </html>
    If i open this with firefox or google chrome i get
    Error. Click for details
    And i also clicked to see the details and there is written
    Java Plug-in 1.6.0_26
    Using JRE version 1.6.0_26-b03 Java HotSpot(TM) Client VM
    User home directory = C:\Users\npiulitza
    ----------------------------------------------------
    c: clear console window
    f: finalize objects on finalization queue
    g: garbage collect
    h: display this help message
    l: dump classloader list
    m: print memory usage
    o: trigger logging
    q: hide console
    r: reload policy configuration
    s: dump system and deployment properties
    t: dump thread list
    v: dump thread stack
    x: clear classloader cache

    load: class C:\Users\npiulitza\Desktop\Folosire not found.
    java.lang.ClassNotFoundException: C:.Users.npiulitza.Desktop.Folosire
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.lang.ClassNotFoundException: C:.Users.npiulitza.Desktop.Folosire

    So... what am i doing wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,337
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Need help to run my first applet

    oad: class C:\Users\npiulitza\Desktop\Folosire not found.
    The code= path should be in "s
    Notice how the path in the error message stops at the space in the path.
    You will have a problem with the package statement. Try removing it for now.

    For simpler testing move all of the .class files and the html file to the same folder.
    After you have removed the package statement and recompiled, change the html Applet tag's code= to:
    code=FirstApplet
    The code= refers to the class name not the filename.

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help to run my first applet

    No, still does not work for me . If i run the applet inside eclipse, it works. But if i try to put it into a HTML code, it does not work. I have moved that .class file to same folder where i have the notepad with html code. I tried all, writing at the
    applet code =
    the path to the file, or only the name of the file, with and without .class extension. Still get that error when i try to open the html file with the browser. I do not understand what am i doing wrong. . I also tried to make another applet to work, and same....cannot make it work

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,337
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Need help to run my first applet

    Did you try this:
    Remove the package statement and compile the program.
    Copy all the .class files created to a test folder.
    Write an HTML file with (I'm assuming the class name is FirstApplet):
    <applet code=FirstApplet width=400 height=400>
    Open the html file in a browser.
    Look in the browser's java console for any error messages.

  5. #5
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help to run my first applet

    Yeah, it worked !! It is good to know i should not create the applet classes inside the packages.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,337
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Need help to run my first applet

    You can put the applet inside a package. For now I thought it'd be simpler to take it out.
    With a package you need to have the correct package path in the code= and the class files must be in the folders that match that package path.

  7. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Need help to run my first applet

    should not create the applet classes inside the packages
    There's nothing wrong with using package names in applets. It's sometimes handy to reduce the number of things that can go wrong (like space characters in non-delimited strings in HTML) by removing package names from your test code, but they do work when used properly!

Similar Threads

  1. Help with this applet.
    By jasonxman in forum Java Applets
    Replies: 1
    Last Post: August 24th, 2011, 01:52 AM
  2. Can't Run my Applet! Please help!
    By JJoelTheMan in forum Java Applets
    Replies: 2
    Last Post: June 10th, 2011, 08:11 AM
  3. when i run applet
    By chonch in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2011, 04:56 PM
  4. applet img
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: April 7th, 2010, 10:14 PM
  5. [SOLVED] applet vs. gui app
    By rptech in forum AWT / Java Swing
    Replies: 3
    Last Post: August 27th, 2009, 10:13 AM