Forums Register Login

Compiles but does not run

+Pie Number of slices to send: Send
I have downloaded java and attempted to run it in the command shell but keep getting the same problem.
I have followed all recommendations I could find on the internet to no effect.
Any code compiles ok but then when running it I get the following error:

java.lang.NoClassDefFoundError: HelloWorldApp/java
Caused by: java.lang.ClassNotFoundException: HelloWorldApp.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Exception in thread "main"

I always execute the program within the directory that it is saved to and have tried setting the CLASSPATH to include the current directory as well as to disinstall and reinstall the environment in different locations and none of this has made any difference. I would very much appreciate it if anyone could let me know what is wrong. Im personally totally clueless. I'm a total beginner.
+Pie Number of slices to send: Send
Hi Karen,

Welcome to JavaRanch!

The argument to "javac" is a *.java file, but the argument to "java" is the name of a class -- not necessarily the name of any file. It looks like you're typing

java HelloWorldApp.java

But instead, you should type

java HelloWorldApp
+Pie Number of slices to send: Send
Thanks alot Ernest, it appears to work now. I can't believe that I went through all of that just because I wrote it with .java insteda of without!
+Pie Number of slices to send: Send
It's common. No worries. I feel your pain

I spent like, maybe 2-3 hrs, maybe more, trying to debug an if test that just had a semicolon where it shouldn't be when I had maybe two weeks-a month experience.
+Pie Number of slices to send: Send
I'm facing the same problem.


Exception in thread "main" java.lang.NoClassDefFoundError: EchoAssociate (wrong
name: com/cognizant/training/EchoAssociate)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)


But i don't have problems with >java EchoAssociate
*I'm actually running a program where I import a class from another folder within the same package.
*I'm NOT getting the same error with a usual single class file.
*I have set the classpath to my user defined location where my source files are. And the package "com" is also present in the same location

following are my 2 programs:

1)


2)



So the package structure is understandable.
+Pie Number of slices to send: Send
Run it at your current directory including your compiled class.
java -classpath . ClassName(replace ClassName using your real class file name)

or paste your "classpath" here.
+Pie Number of slices to send: Send
 

Originally posted by qingwu wang:
Run it at your current directory including your compiled class.
java -classpath . ClassName(replace ClassName using your real class file name)

or paste your "classpath" here.





Sorry Qingwu.. I tried that but it doesn't work for me.
I observed that the problem is with package definition. But i'm unable to figure it out.
Even the simplest program is not working for me just because i include the package statement at the beginning.

Can you pls help with tips and traps to be followed when creating and using a package?
+Pie Number of slices to send: Send
Try compiling with "javac -d . EchoAssociate.java" [You might have to compile Associate first.] That creates a directory (-d) which will have a similar name to the package, finding source files in the present directory (.), and put the .class files in that directory. Now you will have to use the directory name to invoke the class file:
java com.cognizant.training.EchoAssociate
Like that.
+Pie Number of slices to send: Send
Thanks Campbell. It worked for me.
+Pie Number of slices to send: Send
You're welcome, and well done.
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 890 times.
Similar Threads
problems with CLASSPATH
How to use the book CD tester
Running main() in package directory
classpath trouble
EAR precompilation - java.lang.NoClassDefFoundError: weblogic/utils/compiler/Tool
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:48:38.