aspose file tools*
The moose likes Beginning Java and the fly likes Compiles but does not run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiles but does not run" Watch "Compiles but does not run" New topic
Author

Compiles but does not run

Karen Ahlgreen
Greenhorn

Joined: Feb 29, 2008
Posts: 2
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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


[Jess in Action][AskingGoodQuestions]
Karen Ahlgreen
Greenhorn

Joined: Feb 29, 2008
Posts: 2
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!
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
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.


Technology can never substitute for knowledge.
Vinod Ellamaraju
Greenhorn

Joined: Mar 05, 2008
Posts: 3
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.
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
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.


Thanks...qingwu<br />When I open my eyes,I see your pretty face.
Vinod Ellamaraju
Greenhorn

Joined: Mar 05, 2008
Posts: 3
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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
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.
Vinod Ellamaraju
Greenhorn

Joined: Mar 05, 2008
Posts: 3
Thanks Campbell. It worked for me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
You're welcome, and well done.
 
 
subject: Compiles but does not run