Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot run my java code after using Ant

 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI everybody,
I have a weird problem. I have written a java code, which compiles and runs fine on eclipse. However I wanted to run it outside of an ide. i wanted to compile and run it from command prompt. So i used an ant script which compiled and packaged my class files for me. However when i tried to execute the class file generated it gives the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: RunApplication (wrong
name: common/RunApplication)
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)

Please tell me what i am doing wrong!

Further details are as under:

My java source code has the following structure:

src(main folder)
---common(sub folder)
------RunApplication.java


My class files has the following structure:

bin(main folder)
---common(sub folder)
------RunApplication.class


the command i use is
cd bin;
cd common;
java RunApplication

My RunApplication.java is as under


}


 
Henry Wong
author
Marshal
Pie
Posts: 21220
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley Walker wrote:
My class files has the following structure:

bin(main folder)
---common(sub folder)
------RunApplication.class


the command i use is
cd bin;
cd common;
java RunApplication


Try....

cd bin
java common.RunApplication

Henry
 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah tried it. it worked. but i still dont understand why my previous invocation failed.
 
Henry Wong
author
Marshal
Pie
Posts: 21220
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley Walker wrote:yeah tried it. it worked. but i still dont understand why my previous invocation failed.


With "java RunApplication", it will try to run an class name "RunApplication", that is not in any package. And since your classpath is not set, it will look for it in the current directory. It will find it, but when it confirms that the class is name "RunApplication" that is not in any package, it will cause an exception, as the class file is for a class in the common package.

With "java common.RunApplication", it will try to run an class name "RunApplication", that is in the common package. And since your classpath is not set, it will look for it in the common directory. It will find it, and when it confirms that the class is name "RunApplication" that is in common package, it will be confirmed, loaded and ran.

Henry
 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you soo much , crystal clear to me now. some of these small small things often escape us. now that i know it it does seem basic. thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic