Compiling and running a java program in command prompt

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.

Answers


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.


Need Your Help

Problem with Symfony form validation

forms symfony1 validation symfony-1.4

I'm unable to see what I may be doing wrong with the following Symfony 1.4 form validation. Basically, all I just want is for all four conditions to be taken correctly into account (required, min-l...

commandLink/commandButton/ajax backing bean action/listener method not invoked

jsf jsf-2 action commandbutton commandlink

I found a problem when using the <h:commandLink> or <h:commandButton> in an include page: the action and actionListener associated with the UICommand component are simply not being invo...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.