Java Hello World running problem

H

Hongyu

Hi,

I am new to Java and Linux. I have a Linux PC and I tried to write a
simple HelloWorld java program and compile and run it, but I got
errors.
Below is the HelloWorld program:

package world;

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

after compiled it by "javac HelloWorld.java", it passed. And then I
tried to run it by the below command java HelloWorld and got errors.
So I searched on the internet and found that I need to specify the
package name, so I run by the below command:

java world.HelloWorld

But still got errors which was:

Exception in thread "main" java.lang.NoClassDefFoundError: world/
HelloWorld

I also tried to run by the command of "java -cp . HelloWorld", but I
got the same error.

I also tried to do "set CLASSPATH ".:~/workspace/temp", and then run
the program as above, but still get same error, where ~/workspace/temp
is the directory where my HelloWorld.java located.

When I did "which java", I got "/usr/bin/java"; and "which javac", I
got "usr/bin/javac". When I did "ls -l /usr/bin/java*", I got
something like below:

/usr/bin/java --> /etc/alternatives/java
/usr/bin/javac --> /etc/alternatives/javac
.......

When I did echo "CLASSPATH", I got a blank line.
When I did echo $SHELL, I got "/bin/bash"

Could someone kindly tell me how to solve the problem it?

Thanks a lot for the help in advance.

Hongyu
 
H

Hongyu

Hi,

I am new to Java and Linux. I have a Linux PC and I tried to write a
simple HelloWorld java program and compile and run it, but I got
errors.
Below is the HelloWorld program:

package world;

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

}

after compiled it by "javac HelloWorld.java", it passed. And then I
tried to run it by the below command java HelloWorld and got errors.
So I searched on the internet and found that I need to specify the
package name, so I run by the below command:

java world.HelloWorld

But still got errors which was:

Exception in thread "main" java.lang.NoClassDefFoundError: world/
HelloWorld

I also tried to run by the command of "java -cp . HelloWorld", but I
got the same error.

I also tried to do "set CLASSPATH ".:~/workspace/temp", and then run
the program as above, but still get same error, where ~/workspace/temp
is the directory where my HelloWorld.java located.

When I did "which java", I got "/usr/bin/java"; and "which javac", I
got "usr/bin/javac". When I did "ls -l /usr/bin/java*", I got
something like below:

/usr/bin/java --> /etc/alternatives/java
/usr/bin/javac --> /etc/alternatives/javac
......

When I did echo "CLASSPATH", I got a blank line.
When I did echo $SHELL, I got "/bin/bash"

Could someone kindly tell me how to solve the problem it?

Thanks a lot for the help in advance.

Hongyu


Forgot to mention that I have also run by
java -cp . world.HelloWorld", but I
got the same error which was: Exception in thread "main"
java.lang.NoClassDefFoundError: world/
HelloWorld .
 
D

Donkey Hot

Hi,

I am new to Java and Linux. I have a Linux PC and I tried to write a
simple HelloWorld java program and compile and run it, but I got
errors.
Below is the HelloWorld program:

package world;

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

after compiled it by "javac HelloWorld.java", it passed. And then I
tried to run it by the below command java HelloWorld and got errors.
So I searched on the internet and found that I need to specify the
package name, so I run by the below command:

java world.HelloWorld

Java packages are married with filesystem folders. So you have to have a
subfolder world in your current folder, and the HelloWorld.class in that.

world.HelloWordl means world/HelloWorld.class and
world.iceland.HelloWorld means world/iceland/HelloWorld.class

and so on.
 
H

Hongyu

(e-mail address removed):




I am new to Java and Linux. I have a Linux PC and I tried to write a
simple HelloWorld java program and compile and run it, but I got
errors.
Below is the HelloWorld program:
package world;
public class HelloWorld
{
        public static void main(String args[]) throws Exception
        {
                System.out.println("Hello World!");
        }
}
after compiled it by "javac HelloWorld.java", it passed. And then I
tried to run it by the below command java HelloWorld and got errors.
So I searched on the internet and found that I need to specify the
package name, so I run by the below command:
java world.HelloWorld

Java packages are married with filesystem folders. So you have to have a
subfolder world in your current folder, and the HelloWorld.class in that.

        world.HelloWordl means world/HelloWorld.class and
        world.iceland.HelloWorld means world/iceland/HelloWorld.class

and so on.- Hide quoted text -

- Show quoted text -

Thanks Roedy and Donkey for the help. I looked both of them. Since the
article Roedy sited looks more complicated than the Donkey's method,
so i tried Donkey's method first, but if necessary, i will go back to
study more details on the article Roedy recommanded.

Yes, adding a world subfolder to my current folder and move the
HelloWorld.class to that folder and run the command
java world.HelloWorld, it worked! Thanks for the help again.

But I have another question here. I can't always run the progrom in
the current directory, which is ~/workspace/temp, so I would to try
whether i can still run the program in another directory, but
unfortunately I met the similar error again.

That is, I run the below command at the directory of ~/workspace,
instead of ~/workspace/temp, with my HelloWorld.class in the ~/
workspace/temp/world directory by the command:

java temp/world/HelloWorld or java temp.world.HelloWorld or java ~/
workspace/temp/world/HelloWorld, but failed again.

Can anyone kindly help again.

Thanks a lot.
 
J

John B. Matthews

Hongyu said:
Below is the HelloWorld program:

package world;

public class HelloWorld
[...]
Forgot to mention that I have also run by
java -cp . world.HelloWorld", but I
got the same error which was: Exception in thread "main"
java.lang.NoClassDefFoundError: world/
HelloWorld .

$ javac -d . Hello.java
$ java -cp . world/Hello
Hello World!

You should consider using ant.
 
H

Hongyu

Hongyu said:
Below is the HelloWorld program:
package world;
public class HelloWorld
[...]
Forgot to mention that I have also run by
java -cp . world.HelloWorld", but I
got the same error which was: Exception in thread "main"
java.lang.NoClassDefFoundError: world/
HelloWorld .

$ javac -d . Hello.java
$ java -cp . world/Hello
Hello World!

You should consider using ant.

Thanks John. I just tried your suggestion, but still got error:

Exception in thread "main" java.lang.NoClassDefFoundError: world/Hello
 
M

Mark Space

Hongyu said:
java temp/world/HelloWorld or java temp.world.HelloWorld or java ~/
workspace/temp/world/HelloWorld, but failed again.

These fail I think because there is no package and class named
"temp.world.HelloWorld.class" or "workspace.temp.world.HelloWorld.class"
for example.

You have to get Java to look for "world.HelloWorld.class" because that
what you named the package+class. It can't use any other name.

So try:

java -cp ~/workspace/temp world.HelloWorld

That tells Java where to look ("~/workspace/temp") and tells it the
correct name to look for ("world.HelloWorld" which will be in
world/HelloWorld.class under the given classpath).
 
A

Alex.From.Ohio.Java

Hongyu said:
Below is the HelloWorld program:
package world;
public class HelloWorld
[...]
Forgot to mention that I have also run by
java -cp . world.HelloWorld", but I
got the same error which was: Exception in thread "main"
java.lang.NoClassDefFoundError: world/
HelloWorld .

$ javac -d . Hello.java
$ java -cp . world/Hello
Hello World!

You should consider using ant.

Should be:
$ javac -d . Hello.java
$ java -cp . world.HelloWorld

Alex.
http://www.myjavaserver.com/~alexfromohio/
 
D

Donkey Hot

(e-mail address removed):




I am new to Java and Linux. I have a Linux PC and I tried to write
a simple HelloWorld java program and compile and run it, but I got
errors.
Below is the HelloWorld program:
package world;
public class HelloWorld
{
        public static void main(String args[]) throws Exception
        {
                System.out.println("Hello World!");
        }
}
after compiled it by "javac HelloWorld.java", it passed. And then I
tried to run it by the below command java HelloWorld and got
errors. So I searched on the internet and found that I need to
specify the package name, so I run by the below command:
java world.HelloWorld

Java packages are married with filesystem folders. So you have to
have a subfolder world in your current folder, and the
HelloWorld.class in that.

        world.HelloWordl means world/HelloWorld.class and
        world.iceland.HelloWorld means world/iceland/HelloWorld.c lass

and so on.- Hide quoted text -

- Show quoted text -

Thanks Roedy and Donkey for the help. I looked both of them. Since the
article Roedy sited looks more complicated than the Donkey's method,
so i tried Donkey's method first, but if necessary, i will go back to
study more details on the article Roedy recommanded.

Yes, adding a world subfolder to my current folder and move the
HelloWorld.class to that folder and run the command
java world.HelloWorld, it worked! Thanks for the help again.

But I have another question here. I can't always run the progrom in
the current directory, which is ~/workspace/temp, so I would to try
whether i can still run the program in another directory, but
unfortunately I met the similar error again.

That is, I run the below command at the directory of ~/workspace,
instead of ~/workspace/temp, with my HelloWorld.class in the ~/
workspace/temp/world directory by the command:

java temp/world/HelloWorld or java temp.world.HelloWorld or java ~/
workspace/temp/world/HelloWorld, but failed again.

Can anyone kindly help again.

Thanks a lot.

I have not read Roedy's link now, but I think it leads you to the more
advanced tracks on the issue. There will be packaging your classes to a
jar file and Classpath and MANIFEST.MF and such.

You will need those. My advice was really a snap first-aid, and will not
help but only in the very basics.

Your application will eventually contain more than one class file, and
then you figure out that you do not want to deliver
tens/hundreds/thousans of files, but only one. That will be a jar file.

A jar file can be clickable from your favorite file manager.
 
C

Carl

Hongyu said:
Thanks Roedy and Donkey for the help. I looked both of them. Since the
article Roedy sited looks more complicated than the Donkey's method,
so i tried Donkey's method first, but if necessary, i will go back to
study more details on the article Roedy recommanded.

Yes, adding a world subfolder to my current folder and move the
HelloWorld.class to that folder and run the command
java world.HelloWorld, it worked! Thanks for the help again.

But I have another question here. I can't always run the progrom in
the current directory, which is ~/workspace/temp, so I would to try
whether i can still run the program in another directory, but
unfortunately I met the similar error again.

That is, I run the below command at the directory of ~/workspace,
instead of ~/workspace/temp, with my HelloWorld.class in the ~/
workspace/temp/world directory by the command:

java temp/world/HelloWorld or java temp.world.HelloWorld or java ~/
workspace/temp/world/HelloWorld, but failed again.

Can anyone kindly help again.

Thanks a lot.

Hi Hongyu,

Try specifying the directory which holds the base package
directory for the classpath using the -cp switch:

$ java -cp ~/workspace/temp world.HelloWorld
Hello World!

Hope that helps,
Carl.
 
H

Hongyu

(e-mail address removed):
Hi,
I am new to Java and Linux. I have a Linux PC and I tried to write
a simple HelloWorld java program and compile and run it, but I got
errors.
Below is the HelloWorld program:
package world;
public class HelloWorld
{
        public static void main(String args[]) throws Exception
        {
                System.out.println("Hello World!");
        }
}
after compiled it by "javac HelloWorld.java", it passed. And then I
tried to run it by the below command java HelloWorld and got
errors. So I searched on the internet and found that I need to
specify the package name, so I run by the below command:
java world.HelloWorld
Java packages are married with filesystem folders. So you have to
have a subfolder world in your current folder, and the
HelloWorld.class in that.
        world.HelloWordl means world/HelloWorld.class and
        world.iceland.HelloWorld means world/iceland/HelloWorld.c lass
and so on.- Hide quoted text -
- Show quoted text -
Thanks Roedy and Donkey for the help. I looked both of them. Since the
article Roedy sited looks more complicated than the Donkey's method,
so i tried Donkey's method first, but if necessary, i will go back to
study more details on the article Roedy recommanded.
Yes, adding a world subfolder to my current folder and move the
HelloWorld.class to that folder and run the command
java world.HelloWorld, it worked! Thanks for the help again.
But I have another question here. I can't always run the progrom in
the current directory, which is ~/workspace/temp, so I would to try
whether i can still run the program in another directory, but
unfortunately I met the similar error again.
That is, I run the below command at the directory of ~/workspace,
instead of ~/workspace/temp, with my HelloWorld.class in the ~/
workspace/temp/world directory by the command:
java temp/world/HelloWorld or java temp.world.HelloWorld or java ~/
workspace/temp/world/HelloWorld, but failed again.
Can anyone kindly help again.
Thanks a lot.

I have not read Roedy's link now, but I think it leads you to the more
advanced tracks on the issue. There will be packaging your classes to a
jar file and Classpath and MANIFEST.MF and such.

You will need those. My advice was really a snap first-aid, and will not
help but only in the very basics.

Your application will eventually contain more than one class file, and
then you figure out that you do not want to deliver
tens/hundreds/thousans of files, but only one. That will be a jar file.

A jar file can be clickable from your favorite file manager.

Thanks everyone's kind help. I will try all your suggestions and also
read the link Roedy provided and practice it tomorrow in more details
since I don't have my computer with my source code here now.

Thanks again.
 
J

John B. Matthews

Hongyu said:
Hongyu said:
Below is the HelloWorld program:
package world;
public class HelloWorld [...]
Forgot to mention that I have also run by
java -cp . world.HelloWorld", but I
got the same error which was: Exception in thread "main"
java.lang.NoClassDefFoundError: world/
HelloWorld .

$ javac -d . Hello.java
$ java -cp . world/Hello
Hello World!

You should consider using ant.
[...]
Thanks John. I just tried your suggestion, but still got error:

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

Sorry, I misled you. Alex kindly observed that I should have typed

$ java -cp . world.HelloWorld

with a dot instead of an implementation dependent path separator.

[Sometimes I let shell completion go too far! :-]
 
H

Hongyu

 Hongyu said:
<85a3f1ac-c384-4969-a640-8fecc0ced...@f63g2000hsf.googlegroups.com>,
[...]
Below is the HelloWorld program:
package world;
public class HelloWorld
[...]
Forgot to mention that I have also run by
java -cp . world.HelloWorld", but I
got the same error which was: Exception in thread "main"
java.lang.NoClassDefFoundError: world/
HelloWorld .
$ javac -d . Hello.java
$ java -cp . world/Hello
Hello World!
You should consider using ant.
[...]
Thanks John. I just tried your suggestion, but still got error:
Exception in thread "main" java.lang.NoClassDefFoundError: world/Hello

Sorry, I misled you. Alex kindly observed that I should have typed

$ java -cp . world.HelloWorld

with a dot instead of an implementation dependent path separator.

[Sometimes I let shell completion go too far! :-]

No problem and thanks for the help all the same.
 
M

mail oo

can u help to me i am using the netbeans 5.5 .still i have not yet
configure the cvs but last week i tried to configure the CVS i am
facing the lot of problems particularilly

path problem
how to create a user
path and password problem in local host : cvs :check out

how i can configure cvs .......

i am using the following tools

win cvs ,CVSNT,TortoiseCVS-1.10.7,netbeans 5.5

very urgent
 
M

mail oo

can u help to me i am using the netbeans 5.5 .still i have not yet
configure the cvs but last week i tried to configure the CVS i am
facing the lot of problems particularilly

path problem
how to create a user
path and password problem in local host : cvs :check out

how i can configure cvs .......

i am using the following tools

win cvs ,CVSNT,TortoiseCVS-1.10.7,netbeans 5.5

very urgent
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top