Forums Register Login

cannot run my java code after using Ant

+Pie Number of slices to send: Send
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


}


+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
yeah tried it. it worked. but i still dont understand why my previous invocation failed.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 849 times.
Similar Threads
Class path magic
Package
Exception in thread "main"
Test class cannot find other class
Ant unable to find javac compiler (in Eclipse)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:52:58.