aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem running java program:NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem running java program:NoClassDefFoundError" Watch "Problem running java program:NoClassDefFoundError" New topic
Author

Problem running java program:NoClassDefFoundError

Vs Kommuru
Greenhorn

Joined: Mar 31, 2010
Posts: 7
Hi,

Recently, I have started preparing for the exam. I am getting an error

Exception in thread "main" java.lang.NoClassDefFoundError: Test2
Caused by: java.lang.ClassNotFoundException: Test2
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)
Could not find the main class: Test2. Program will exit.

When I try to run the following code




my intention is to use the setDisp protected method of Test1 class of govin package in Test2 class of yana package.
please let me know my mistake. Thanks
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
How are you compiling this program? I hope you are giving the correct class name for Test2 class while compiling it.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
I see this problem a lot with my Java students. If they try to run a class by using an IDE, they forgot to "Run As... Java Application". All they did was "Run" and the same error occurs.


SCJP 6
Vs Kommuru
Greenhorn

Joined: Mar 31, 2010
Posts: 7
please go through the following compilation and running process

D:\java>cd govin

D:\java\govin>javac Test1.java

D:\java\govin>cd..

D:\java>cd yana

D:\java\yana>javac Test2.java

D:\java\yana>java Test2
Exception in thread "main" java.lang.NoClassDefFoundError: Test2
Caused by: java.lang.ClassNotFoundException: Test2
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)
Could not find the main class: Test2. Program will exit.

D:\java\yana>type Test2.java
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

You are running the class in wrong way.

If you see your class Test2 is present in the package yana.

From the command prompt give the command as

java yana.Test2

P.S. Please use meaningful subject lines while posting the question, so it becomes easy for the reader.

Hope this helps.


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Try these commands

D:\java>javac govin\Test1.java

D:\java>javac yana\Test2.java

D:\java>java yana.Test2

[Okay so I was beaten by 25 seconds, so what ]


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vs Kommuru
Greenhorn

Joined: Mar 31, 2010
Posts: 7
Thanks for correcting me. By mistake, I have forgotten.

Thanks,
Venkata
Vs Kommuru
Greenhorn

Joined: Mar 31, 2010
Posts: 7

Please let me know why cant I run the class from the package directory. Is it beacuse of using the package name in the program or any other reason.

Thanks,
Venkata
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

VenkataS Kommuru wrote:Please let me know why cant I run the class from the package directory.

When you run a program, you need the fully qualified name of the class (i.e. package name + class name). If your class' fully qualified name is yana.Test2, then JVM will look for class named Test2 in yana directory of the current directory. This is why you need to be in the parent directory of yana to run Test2 class...
Vs Kommuru
Greenhorn

Joined: Mar 31, 2010
Posts: 7
Thanks alot !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem running java program:NoClassDefFoundError