• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem running java program:NoClassDefFoundError

 
Vs Kommuru
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vs Kommuru
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vs Kommuru
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting me. By mistake, I have forgotten.

Thanks,
Venkata
 
Vs Kommuru
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic