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

Basic package problems

Riley Thomas
Greenhorn

Joined: Aug 08, 2009
Posts: 13
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

"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.


[My Blog]
All roads lead to JavaRanch
Riley Thomas
Greenhorn

Joined: Aug 08, 2009
Posts: 13
Sorry, still learning my way around here.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Aug 08, 2009
Posts: 13
Great - thanks for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic package problems