0

I have written a simple hello world program given below

package helloworld;

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

I'm using Windows 7 and have set my CLASSPATH and PATH variables in the environment variables window as

C:\Program Files\Java\jdk1.6.0_24\bin;

The program is located at

C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld

So, basically when i try to run it in my command prompt, the following happens

C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld>javachelloWorld.java

C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld>java helloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: helloWorld (wrong name: helloworld/helloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: helloWorld.  Program will exit.

The program seems to be compiling fine and does not throw any errors. But when i try to run it, it says than exceptions have occurred. I tried looking up on Google thinking that setting my PATH and CLASSPATH was wrong, but I don't seem to find any issues with that.

2
  • have you defined any package ? Commented Feb 27, 2014 at 4:46
  • @Kakarot Yes I have. Sorry, Edited the question now. Commented Feb 27, 2014 at 4:48

2 Answers 2

2

In case your helloWorld.java file is in a package, you can run as follows :

C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\
java packageName.helloWorld

Run the code from outside the helloworld package directory i.e from src directory (C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src)

Also check if the name of your java file is the same as your class name (helloWorld.java)

Also it seems that the name of your package is helloworld (all in small letters) however the folder name is helloWorld (note that W is capital) both the names need to match.

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you. I just tried it out. But the same exceptions are being thrown by it
try running the code from inside helloworld package directory
Check the name of the java file is it same as class file ?
Yes, both are named helloWorld. Both helloWorld.java and helloWorld.class exist in C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld
did you try running the code from helloWorld folder or src folder
|
0

You need to make sure you are in the directory containing your helloWorld.class file when you issue the command

java helloWorld

3 Comments

Yeah, I reset my CLASSPATH to the director where my helloWorld.class exists. Tried running it again. Still throwing the same exceptions.
I mean using cd and cd.. in the command prompt. Navigate to the directory that is holding your helloWorld.class file before issuing the java command
The helloWorld.class file has been created in the same folder on compilation C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld So, I dont need to change the directory, right?

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.