What is wrong with my java applet ?

I used notepad to write a java applet as follows :

class addition {
public static void main (String args[]){
System.out.println(2+2);
}

Then saved it as addition.java.
Then javac addition.java. It gave me addition.class.
Then java addition
But an error message ocurred as follows:
Exception in thread "main" java.lang.NoClassDefFoundError:c:\addition/class

My question is :
1. What is the meaning of the error msg
2. How to make my program work ?
man_15Asked:
Who is Participating?
 
CircleVConnect With a Mentor Commented:
Well, if your code sample is exactly the contents of addition.java, then I'm surprised it compiled, as you're missing a close brace.  ie the code should
be:

class addition {
    public static void main (String args[]){
        System.out.println(2+2);
    }
}

and as sgoms said,

compile to a .class file using "javac addition.java"
and run it by using "java addition"

note also that this is actually a java application, and not an Applet.  The Oreilly book "Java in a Nutshell" has a pretty decent description of this.

as for the meaning of the message, it means that the java interpreter cannot find your .class file.  The interpreter looks in your classpath for the .class files it needs.  It has a default path which includes your current directory, so the above should work if you run "java addition" in the same directory as addition.class.

happy coding!

CircleV
0
 
_lychee_Commented:
u sure u used
java addition

and not
java addition.class
or
java c:\addition.class??

(ur error message sounds as if u were using java c:\addition.class)

ur errormessage means that the jvm was not able to find the class
c:\addition/class which i think refers to the class "class" in the package "c:\addition"...
0
 
sgomsCommented:
1. First this is not an applet for a java application

2. and yes the above error indicates
    java addition.class

the procedure is,

javac addition.java > This compiels ur java source file to a set of byte codes put into the class file

java addition > (without .class) inteprets(or compiles with Just in time compiler is present) the byte code to native code.

happy programming!
-sgoms

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
sgomsCommented:
1. First this is not an applet but a java "application"

2. and yes the above error indicates
    java addition.class

the procedure is,

javac addition.java > This compiels ur java source file to a set of byte codes put into the class file

java addition > (without .class) inteprets(or compiles with Just in time compiler is present) the byte code to native code.

happy programming!
-sgoms

0
 
man_15Author Commented:
Thanks u guys!
Originally my addition.class was in another directory. But then I put the file back into jdk1.2. It works.
I have two questions :
(1) Can addition.class be put in another directory other than jdk1.2 ?
(2) I was using dosprompt in win98. But the outlook is not good. Also the filename follows the 8+3 format. Is there any other area or application I can use to do "java addition" ?
0
 
sgomsCommented:
man_15,

Yes. your java/class file can be in another directory.
what you need to set is something called as a PATH/CLASSPATH.

in your autoexec.bat file,

set PATH=<java_home>\bin;
set CLASSPATH=<java_home>\lib;

java_home cud be c:\jdk1.2.2 or any other directory where you have installed java.

PATH:
when you set your path, any excutable file under that directory is avaialble for execuetion under other directories too.
so you can access java.exe & javac.exe from other directories.

CLASSPATH:
when you invoke java/javac the class files need for compilation,
like (java.lang.* or java.awt.*) will be first located from this classpath directory & used.
JDK1.2 has a few jar files under <java_home>\lib which contain these java libraries.

make sure to execute c:\autoexec after makeing the changes.

2. There are quite a few IDE's that you can use for java. JBuilder, VJ++, Visual Cafe, Symantic Cafe, Visual Age etc.. are pretty good!
There are some small time IDE's which will have very basic options like Jpadpro, sitepadpro, kawa etc..

if you want expert opinion on what is best, there are quite a few outthere. so u can post another question seperately on this topic.

all the best!
-sgoms
0
 
heyhey_Commented:
very short comment

SET CLASSPATH=.;%CLASSPATH%
java myApp
0
All Courses

From novice to tech pro — start learning today.