Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

newbie: cannot execute HelloWorld

4 views
Skip to first unread message

R.A.M.

unread,
Dec 27, 2006, 2:45:11 PM12/27/06
to
Hello,
I have started learning Java. I have installed Java with NetBeans 5.0. In
NetBeans I have written Hello World application which I can run from IDE
correctly. But when I tried to run it from command prompt (> java
Main.class) I receive:

Exception in thread "main" java.lang.NoClassDefNotFoundError: Main/class

Could you explain me the problem and could you tell me how to solve it?
Thank you!
/RAM/


Derek Tandy

unread,
Dec 27, 2006, 2:50:57 PM12/27/06
to
Is your java file called Main.java or is it HelloWorld.java?

Thomas Kellerer

unread,
Dec 27, 2006, 2:54:03 PM12/27/06
to

R.A.M.

unread,
Dec 27, 2006, 3:23:00 PM12/27/06
to
Main.java

Uzytkownik "Derek Tandy" <kra...@gmail.com> napisal w wiadomosci
news:1167249057.4...@48g2000cwx.googlegroups.com...

R.A.M.

unread,
Dec 27, 2006, 3:23:58 PM12/27/06
to
java Main produces:
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name:
helloworld/Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)

Please help...

Uzytkownik "Thomas Kellerer" <TAAXAD...@spammotel.com> napisal w
wiadomosci news:4vg1arF...@mid.individual.net...

R.A.M.

unread,
Dec 27, 2006, 3:25:31 PM12/27/06
to
Here source code:

package helloworld;
public class Main {
public Main() {
}
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}


Użytkownik "R.A.M." <r_ahi...@poczta.onet.pl> napisał w wiadomości
news:emuig9$fhr$1...@news.onet.pl...

heys...@gmail.com

unread,
Dec 27, 2006, 5:40:30 PM12/27/06
to
You need to move it into a folder called helloworld (i.e.
c:\helloworld\Main.class) because its in that package (package refers
to directory location) and then you can run from the folder above
helloworld using "java helloworld.Main".

Scott

On Dec 27, 12:25 pm, "R.A.M." <r_ahims...@poczta.onet.pl> wrote:
> Here source code:
>
> package helloworld;
> public class Main {
> public Main() {
> }
> public static void main(String[] args) {
> System.out.println("Hello, world!");
> }
>

> }U¿ytkownik "R.A.M." <r_ahims...@poczta.onet.pl> napisa³ w wiadomo¶cinews:emuig9$fhr$1...@news.onet.pl...

jupiter

unread,
Dec 27, 2006, 6:48:56 PM12/27/06
to

"R.A.M." <r_ahi...@poczta.onet.pl> wrote in message
news:emukp1$n1q$1...@news.onet.pl...

> java Main produces:
> Exception in thread "main" java.lang.NoClassDefFoundError: Main
> (wrong name: helloworld/Main)
> at java.lang.ClassLoader.defineClass1(Native Method)
> at java.lang.ClassLoader.defineClass(Unknown Source)
> at java.security.SecureClassLoader.defineClass(Unknown
> Source)
> at java.net.URLClassLoader.defineClass(Unknown Source)
> at java.net.URLClassLoader.access$100(Unknown Source)
> 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)
>

See the second line where it says "wrong name helloworld/Main"?
That's because you've got a package name and you're not using it to
invoke the class. You need to include the package name as part of
the class name with a dot separator (after copying the file to the
correct location.)

Example:
1. Source and compiled class are currently in
c:\JavaProjects\MainSample
2. Create a directory under MainSample named helloworld. Package
names require the compiled class to be in a named subdirectory.
3. Copy Main.class to helloworld.
4. From the source directory type:
java -classpath c:\JavaProjects\MainSample helloworld.Main

Note that helloworld.Main is not just Main. It can be considered
like a "virtual class name". It includes the package. That's how
the JVM knows which class you want to run (you could have Main
classes in other packages.)

The -classpath argument tells the compiler the ROOT to the top
level directory of the package. So *after* the classpath arg you
must provide the the class name, but since you have a package in
the name it has to include that too. The oddity is that you don't
give the JVM helloworld/Main, you give it helloworld.Main. The JVM
figures out that the dot separator indicates a backslash or slash.

There is a lot more to know about classpaths too. You can get into
a lot of trouble by ignoring classpath issues. Once you start
using jar files it changes things, so don't rush past learning
about packages and classpath now.

0 new messages