newbie: cannot execute HelloWorld

R

R.A.M.

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/
 
T

Thomas Kellerer

R.A.M. wrote on 27.12.2006 20:45:
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/


leave out .class, just use "java Main"

More information here:

http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html

http://java.sun.com/docs/books/tutorial/index.html
 
R

R.A.M.

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

R.A.M.

Here source code:

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

heysc0tt

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

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. said:
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/
 
J

jupiter

R.A.M. said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top