1

I'm trying to run a code on lifecycle of applet as shown. This file is saved as Lifecycle.java

I compiled it by

javac Lifecycle.java

then tried to run it by

appletviewer Lifecycle.java

package APPLETS;
import java.applet.Applet;

public class Lifecycle extends Applet
 {

    /*
    < APPLET
    code = "Lifecycle.class"
    height = "300"
    width = "300">
    < \APPLET>
    */

public void init() 
  {System.out.print("INIT");}

public void stop() 
  {System.out.print("STOP");}

public void start() 
  {System.out.print("Start");}

public void destroy() 
  {System.out.print("Destroy");}

}

APPLET is not loading then, though my code compiles successfully, no instructions are seen on command prompt. I'm just seeing a blank page with error -> Start:applet not initialized

HERE is the Lifecycle.html code-->

and here is the ERROR-

load: class APPLETS.Lifecycle.class not found. java.lang.ClassNotFoundException: APPLETS.Lifecycle.class

Roman C
  • 49,761
  • 33
  • 66
  • 176
DD24
  • 61
  • 7
  • Please see http://stackoverflow.com/questions/4951695/receiving-wrong-name-noclassdeffounderror-when-executing-a-java-program-from-t/12044735#12044735 – Mark W May 31 '13 at 17:47

2 Answers2

1

The appletviewer is expecting to find HTML content so cannot parse the input file. Use appletviewer against a URL rather than a Java source file.

appletviewer is used to view applets using a URL. This URL can be in the format of a local or remote HTML document. Create a HTML document including the tag specifying your class and run the appletviewer against it.

life.html:

<APPLET CODE="APPLETS.Lifecycle" width="300" height="300"></APPLET>

then use

appletviewer life.html

The simplest folder structure for this to run is

./
 |life.html
 |-APPLETS 
    Lifecycle.class

Related: The Java Applet Viewer

Aside: Consider using the more up-to-date Swing JApplet.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • <\APPLET> here shudn't i add the extension .java ...?? ie – DD24 May 31 '13 at 18:16
  • sorry to say bro .. but m still getting the same problem..! – DD24 May 31 '13 at 18:18
  • No the extension is optional and it is the `.class` file that you need. Is the error msg the same? Do you have the folder structure as above? – Reimeus May 31 '13 at 18:20
  • yess the structure is exactly the same as above.. and the error is till the same .. here it is.. – DD24 May 31 '13 at 18:23
  • F:\JavaP\APPLETS>appletviewer Lifecycle.html load: class APPLETS.Lifecycle.java not found. java.lang.ClassNotFoundException: APPLETS.Lifecycle.java 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) – DD24 May 31 '13 at 18:25
  • Why do you have class `APPLETS.Lifecycle.java`? That should be `APPLETS.Lifecycle.class` or simply `APPLETS.Lifecycle` in the applet tag – Reimeus May 31 '13 at 18:27
  • ohh ya sorry ... it is APPLETS.Lifecycle.class i was wrote it wrong here.. it is correct in the actual code.. – DD24 May 31 '13 at 18:37
  • the code runs succesfully when i tried it in Java ide netbeans.. why its not running in cmdprompt..using appletviewer comand ..hv any idea? – DD24 May 31 '13 at 18:39
  • 2 issues 1.) CODE attribute missing from tag 2) backslash in closing tag should be forward slash. As updated – Reimeus May 31 '13 at 18:48
  • Issues solved.. But the problem still persists..M sorry ..i know m troubling u .. :) – DD24 May 31 '13 at 19:12
  • another thing ..i made 5 more programs on applet .. which successfully through cmd appletviewer command .. only this program has problem .. though it is the simplest of all possible applet programs.. :/ – DD24 May 31 '13 at 19:13
  • Can you attach the error and your life.html content to the question? Ensure that the class `Lifecycle.class` is in the _APPLETS_ folder. – Reimeus May 31 '13 at 20:09
  • The class file is not where its expected to be. 1) Are you running appletviewer from the same directory as life.html? Is `Lifecycle.class ` in the `APPLETS` directory relative to that dir? I tested this on my machine & works fine so I reckon its something very small... – Reimeus Jun 01 '13 at 12:33
  • yeahh .. m running appletviewer from the same directory as Lifecycle.html ...Lifecycle.class also is present in APPLETS package along wid Lifecycle.html and Lifecycle.java.. n yess may be you r right .. its sumthng very small ..and annoying too :/ – DD24 Jun 01 '13 at 13:53
  • do we have the set the path of appletviewer or sumthng like that ..bcoz i think that theres some problem with the path which is to be specified sumwhere.. – DD24 Jun 01 '13 at 14:01
  • No it should find the class from the class tag in the html file – Reimeus Jun 01 '13 at 14:02
0

Put Lifecycle.java in a folder called APPLETS, and try running:

appletviewer APPLETS.Lifecycle
Alex Gittemeier
  • 5,224
  • 30
  • 55