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

Command prompt session:

C:\Users\zobdos\Desktop>javac HelloWorld.java

C:\Users\zobdos\Desktop>dir *.class
 Volume in drive C is OS
 Volume Serial Number is A45E-7B01

 Directory of C:\Users\zobdos\Desktop

11/20/2010  10:16 AM               417 HelloWorld.class
               1 File(s)            417 bytes
               0 Dir(s)   8,145,432,576 bytes free

C:\Users\zobdos\Desktop>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: HelloWorld.  Program will exit.

C:\Users\btolbert\Desktop>
2
  • Note: in the HelloWorld.java file a Public HelloWorld java class MUST be defined. Commented Nov 20, 2010 at 16:28
  • @andreas: no, the class doesn't need to be public. Default visibility is also fine. It's only not visible to classes in other packages. Commented Nov 20, 2010 at 16:39

3 Answers 3

3

Nevermind, it works after using:

java -classpath . HelloWorld
Sign up to request clarification or add additional context in comments.

Comments

3

You've a %CLASSPATH% environment variable in your environment. Get rid of it, it's disturbing your java commands, it's a poor practice tought by Sun Oracle anyway. Once you use -classpath argument or its -cp shorthand, then the %CLASSPATH% will be overridden. If the classpath is not specified by either the environment variable or the arguments, then the current path will be taken as default (as you initially expected).

Comments

2

run with classpath specified to the current directory:

java -cp . HelloWorld

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.