aspose file tools*
The moose likes Beginning Java and the fly likes cannot run my java code after using Ant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cannot run my java code after using Ant" Watch "cannot run my java code after using Ant" New topic
Author

cannot run my java code after using Ant

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
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
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
yeah tried it. it worked. but i still dont understand why my previous invocation failed.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

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

Joined: Sep 23, 2009
Posts: 87
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cannot run my java code after using Ant