Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Hello World running problem

Reply
Thread Tools

Java Hello World running problem

 
 
Hongyu
Guest
Posts: n/a
 
      07-29-2008
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-29-2008
On Tue, 29 Jul 2008 14:11:28 -0700 (PDT), Hongyu <>
wrote, quoted or indirectly quoted someone who said :

>
>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:


see http://mindprod.com/jgloss/helloworld.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
Hongyu
Guest
Posts: n/a
 
      07-29-2008
On Jul 29, 5:11*pm, Hongyu <hongyu...@yahoo.com> wrote:
> 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 .



 
Reply With Quote
 
Donkey Hot
Guest
Posts: n/a
 
      07-29-2008
Hongyu <> wrote in news:7970c0c6-6c08-43d6-a55e-
:

> 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.
 
Reply With Quote
 
Hongyu
Guest
Posts: n/a
 
      07-29-2008
On Jul 29, 5:28*pm, Donkey Hot <s...@plc.is-a-geek.com> wrote:
> Hongyu <hongyu...@yahoo.com> wrote in news:7970c0c6-6c08-43d6-a55e-
> 0dc114d7a...@m3g2000hsc.googlegroups.com:
>
>
>
>
>
> > 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.- 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.
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      07-29-2008
In article
<85a3f1ac-c384-4969-a640->,
Hongyu <> wrote:

> On Jul 29, 5:11*pm, Hongyu <hongyu...@yahoo.com> wrote:

[...]
> > 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.

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
 
Reply With Quote
 
Hongyu
Guest
Posts: n/a
 
      07-29-2008
On Jul 29, 5:58 pm, "John B. Matthews" <nos...@nospam.com> wrote:
> In article
> <85a3f1ac-c384-4969-a640-8fecc0ced...@f63g2000hsf.googlegroups.com>,
>
>
>
> Hongyu <hongyu...@yahoo.com> wrote:
> > On Jul 29, 5:11 pm, Hongyu <hongyu...@yahoo.com> wrote:

> [...]
> > > 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.
>
> --
> John B. Matthews
> trashgod at gmail dot com
> home dot woh dot rr dot com slash jbmatthews


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

Exception in thread "main" java.lang.NoClassDefFoundError: world/Hello
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-29-2008
Hongyu wrote:

> 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).
 
Reply With Quote
 
Alex.From.Ohio.Java@gmail.com
Guest
Posts: n/a
 
      07-29-2008
On Jul 29, 4:58 pm, "John B. Matthews" <nos...@nospam.com> wrote:
> In article
> <85a3f1ac-c384-4969-a640-8fecc0ced...@f63g2000hsf.googlegroups.com>,
>
>
>
> Hongyu <hongyu...@yahoo.com> wrote:
> > On Jul 29, 5:11 pm, Hongyu <hongyu...@yahoo.com> wrote:

> [...]
> > > 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.
>
> --
> John B. Matthews
> trashgod at gmail dot com
> home dot woh dot rr dot com slash jbmatthews


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

Alex.
http://www.myjavaserver.com/~alexfromohio/
 
Reply With Quote
 
Donkey Hot
Guest
Posts: n/a
 
      07-29-2008
Hongyu <> wrote in
news:78336657-ad1b-416f-ba90-:

> On Jul 29, 5:28*pm, Donkey Hot <s...@plc.is-a-geek.com> wrote:
>> Hongyu <hongyu...@yahoo.com> wrote in news:7970c0c6-6c08-43d6-a55e-
>> 0dc114d7a...@m3g2000hsc.googlegroups.com:
>>
>>
>>
>>
>>
>> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WORLD TOURSIM WORLD TRAVEL WORLD PACKAGE TAJMAHAL TEMPLE Java 0 04-07-2008 03:34 PM
WORLD TOURSIM WORLD TRAVEL WORLD PACKAGE TAJMAHAL TEMPLE Python 0 04-07-2008 03:26 PM
regular expression to parse {"hello", "hello world","1hello-2*hello"} Roy Java 6 01-07-2008 08:06 PM
to print in the reverse order, ("Hello World" -> "World Hello") vijay C Programming 8 04-26-2005 02:11 AM
The world's shortest 'Hello World!' program: a proposal Larry Perl Misc 27 01-25-2005 07:53 PM



Advertisments