Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic package problems

 
Riley Thomas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I can't seem to sort out this error using very basic packages - I think this may be resolved with a flag with javac, but first things first.

package hello;

public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
}

}

Compiles fine, but :$ java HelloWorld produces:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: hello/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
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)

:$ java hello.HelloWorld produces:
Exception in thread "main" java.lang.NoClassDefFoundError: hello/HelloWorld

I know this is so basic and I've worked with packages previously, just not on my home machine (running 1.5 under OS X) before.

Thanks-
DJ
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Dj Percent",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (obviously fake name are not allowed). Thank you.
 
Riley Thomas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, still learning my way around here.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to refer to HelloWorld via its fully qualified name. The easiest way is to go to the parent directory of the hello package and call : java -cp . hello.HelloWorld

(Thanks for changing your name )
 
Riley Thomas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great - thanks for your help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic