• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiles but does not run

 
Karen Ahlgreen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Karen Ahlgreen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinod Ellamaraju
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinod Ellamaraju
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell. It worked for me.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, and well done.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic