"java Hello" Problem

B

buts101

Hello there,
I wrote thise helloworld code in java as Hello.java

----------------------------
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
------------------------------
Then i compiled it with
->javac Hello.java

It compilers normally,no error showned

I run the Hello.class file as

->java Hello
Then i get this error message:
->Exception in thread "main" java.lang.NoClassDefFoundError: hello

Plz tell me what is the error and how can i run that and other class
file.

Waiting for quick reply

Kind Regards,
Buts101
 
A

Andrew Thompson

buts101 wrote:

Please always copy/paste input and output. Always.
->java Hello
Then i get this error message:
->Exception in thread "main" java.lang.NoClassDefFoundError: hello

Because that could not be true.

It might be..
->java Hello
Then i get this error message:
->Exception in thread "main" java.lang.NoClassDefFoundError: Hello

...or ..
->java hello
Then i get this error message:
->Exception in thread "main" java.lang.NoClassDefFoundError: hello

...both of which are different.

Andrew T.
 
A

Andrew Thompson

Alex said:
That's a little presumptuous, given that people are helping here
voluntarily.

Thanks for reminding me. I had meant to
comment on that.

Perhaps next time, if the OP might 'slip a 50',
into the post - I might take them seriously.

Andrew T.
 
L

Lothar Leidner

buts101 said:
Hello there,
I wrote thise helloworld code in java as Hello.java

----------------------------
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
------------------------------
Then i compiled it with
->javac Hello.java

It compilers normally,no error showned

I run the Hello.class file as

->java Hello
Then i get this error message:
->Exception in thread "main" java.lang.NoClassDefFoundError: hello

You named your class "Hello" (not ("hello").

Regards,

Lothar Leidner
 
L

Lew

Lothar said:
You named your class "Hello" (not ("hello").

Now, let's assume you do this:

java Hello

and see this:

Exception in thread "main" java.lang.NoClassDefFoundError: Hello

That would indicate that you have not given 'java' the classpath information
it needs. The classpath is a list of directories that are at the root of your
class directory hierarchies, and of individual JAR files. It follows the same
syntax as other path variables in your operating system, such as PATH.

(Note: the classpath does not include directories that contain JARs, only
directory tree roots that contain .class files and references to the JAR files
themselves. For example, assuming UNIX,
"/somedirectory/classroot:/someotherdirectory/path/necessary.jar".)

What is your current working directory (cwd) when you execute "java Hello"?

You likely will need to specify:

java -cp directoryAtRootOfYourClassTree Hello

which, if your cwd is the directory where your default-package classes reside,
would be:

java -cp . Hello

for example.

-Lew
 
B

buts101

Input and Output are:
E:\Program Files\Java\jdk1.5.0_07\bin>javac Hello.java

E:\Program Files\Java\jdk1.5.0_07\bin>java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello

E:\Program Files\Java\jdk1.5.0_07\bin>
 
A

Andrew Thompson

buts101 wrote:

Please refrain from top-posting.
Input and Output are:
E:\Program Files\Java\jdk1.5.0_07\bin>javac Hello.java
...
Could you add a line in here, at this point..

E:\Program Files\Java\jdk1.5.0_07\bin>dir Hello*

...what is the output?
E:\Program Files\Java\jdk1.5.0_07\bin>java Hello
...

Andrew T.
 
P

Paul Hamaker

Maybe you included a package name that you forgot to mention, as in :
java somepack.Hello
 
L

Lew

Paul said:
Maybe you included a package name that you forgot to mention, as in :
java somepack.Hello

He's running java from the bin/ directory where java is, and he ignored the
advice about setting the classpath.
E:\Program Files\Java\jdk1.5.0_07\bin>java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello

Normally one does not use the program directory as the cwd but a project
directory, and has the Java executables in the PATH.

Either way, pay attention to the earlier advice about setting the classpath
parameter.
Input and Output are:
E:\Program Files\Java\jdk1.5.0_07\bin>javac Hello.java

Under no circumstances should you ever, ever, ever put your work in the Java
program directory tree! Ever!

- Lew
 
J

jupiter

Lew said:
Under no circumstances should you ever, ever, ever put your work
in the Java program directory tree! Ever!

This reminds me of the old saying about "cruisin' for a bruisin'".
 
K

kmastanreddy

first set the classpath correctly suppose i installed the jdk1.5
software in D: directory i want to set the classpath here like this
D:\>set classpath=:"D:\jdk1.5.0_05\bin"; in command prompt
or
set in my computer>properties>advance>enviornment variables>set path as
D:\jdk1.5.0_05\bin; like that

because java.lang is default loader of class file in case of se the
classpath correctly

then run the program correctly

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

then compile the file D:\javac Hello1.java
then run the file D:\java Hello1

in case of u save the file in the D directory.
 
C

ck

Input and Output are:
E:\Program Files\Java\jdk1.5.0_07\bin>javac Hello.java

E:\Program Files\Java\jdk1.5.0_07\bin>java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello

E:\Program Files\Java\jdk1.5.0_07\bin>

In case it still fails could you paste the output of set classpath
Eg:-

E:\Program Files\Java\jdk1.5.0_07\bin\set classpath


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


Try these steps:
========================================
E:\Program Files\Java\jdk1.5.0_07\bin\set classpath=;
E:\Program Files\Java\jdk1.5.0_07\bin>javac Hello.java
E:\Program Files\Java\jdk1.5.0_07\bin>java Hello

=========================================
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top