Exception in thread

package javaapplication1;

import java.io.*;

publicclass InputDemo{

/** Creates a new instance of InputDemo */

public String readStr()throws IOException{

InputStreamReader isr =new InputStreamReader(System.in);

BufferedReader br =new BufferedReader(isr);

return br.readLine();

}

publicint readInt()throws IOException, NumberFormatException{

return Integer.parseInt(readStr());

}

publicstaticvoid main(String args[]){

InputDemo ipd =new InputDemo();

String str ="";

int a = 0;

try{

System.out.print("Enter a string :");

str = ipd.readStr();

System.out.print("Enter a number: ");

a = ipd.readInt();

System.out.println("The recently inputted string: " + str);

System.out.println("The recently inputed number: " + a);

}catch(IOException e1){}

catch(NumberFormatException e2){}

}

}

When i complie this above snippet of code, no problem. But when i run it, i got problem as follow:

E:\Java Programmes>javac InputDemo.java // no problem with compiling

E:\Java Programmes>java InputDemo//problem with running

Exception in thread "main" java.lang.NoClassDefFoundError: InputDemo (wrong name

: javaapplication1/InputDemo)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.access$100(Unknown Source)

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

And i try to find the cause underlying the problem. And when i remove the line "package javaapplication1;" , i could solve the problem. I cannot understand why it is like that. Anyone can explain it to me. Thanks for your teaching.

[3526 byte] By [beckham12a18a] at [2007-10-1 16:25:10]
# 1

When you compile and run classes that are in a package, you need to use the fully qualified class name, that is, the name that includes the package name and use a directory structure that conincides with your package structure.

You should put the file InputDemo.java in a directory called javaapplication1 and then compile and run with the commandsC:\somedir>javac javaapplication1/InputDemo.java

C:\somedir>java javaapplication1.InputDemo

jsalonena at 2007-7-11 0:39:54 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

> When you compile and run classes that are in a

> package, you need to use the fully qualified class

> name, that is, the name that includes the package

> name and use a directory structure that conincides

> with your package structure.

>

> You should put the file InputDemo.java in a directory

> called javaapplication1 and then compile and run with

> the commandsC:\somedir>javac

> javaapplication1/InputDemo.java

> C:\somedir>java javaapplication1.InputDemo

Thanks to your direction i got it. However, i'd like to add some things.

C:\somedir>javac javaapplication1\InputDemo.java

C:\somedir>java javaapplication1.InputDemo

The above -mentioned approach is used to compile file in package only if the directory whose name is package name has been already created and the .java file must be placed in the directory (package name).

As to the another approach using conditonal compiling as follow:

C:\somedir>javac -d "C:\somedir" InputDemo.java

C:\somedir>java javaapplication1.InputDemo

Anyway, thanks a lot for your help.

beckham12a18a at 2007-7-11 0:39:54 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...