Java Hello World running problem

Discussion in 'Java' started by Hongyu, Jul 29, 2008.

  1. Hongyu

    Hongyu Guest

    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
    Hongyu, Jul 29, 2008
    #1
    1. Advertising

  2. Hongyu

    Roedy Green Guest

    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
    Roedy Green, Jul 29, 2008
    #2
    1. Advertising

  3. Hongyu

    Hongyu Guest

    On Jul 29, 5:11 pm, Hongyu <> 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 .
    Hongyu, Jul 29, 2008
    #3
  4. Hongyu

    Donkey Hot Guest

    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.
    Donkey Hot, Jul 29, 2008
    #4
  5. Hongyu

    Hongyu Guest

    On Jul 29, 5:28 pm, Donkey Hot <-a-geek.com> wrote:
    > 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.- 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.
    Hongyu, Jul 29, 2008
    #5
  6. In article
    <>,
    Hongyu <> wrote:

    > On Jul 29, 5:11 pm, Hongyu <> 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
    John B. Matthews, Jul 29, 2008
    #6
  7. Hongyu

    Hongyu Guest

    On Jul 29, 5:58 pm, "John B. Matthews" <> wrote:
    > In article
    > <>,
    >
    >
    >
    > Hongyu <> wrote:
    > > On Jul 29, 5:11 pm, Hongyu <> 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
    Hongyu, Jul 29, 2008
    #7
  8. Hongyu

    Mark Space Guest

    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).
    Mark Space, Jul 29, 2008
    #8
  9. Hongyu

    Guest

    On Jul 29, 4:58 pm, "John B. Matthews" <> wrote:
    > In article
    > <>,
    >
    >
    >
    > Hongyu <> wrote:
    > > On Jul 29, 5:11 pm, Hongyu <> 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/
    , Jul 29, 2008
    #9
  10. Hongyu

    Donkey Hot Guest

    Hongyu <> wrote in
    news::

    > On Jul 29, 5:28 pm, Donkey Hot <-a-geek.com> wrote:
    >> 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.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.
    Donkey Hot, Jul 29, 2008
    #10
  11. Hongyu

    Carl Guest

    Hongyu <> writes:
    > 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.
    Carl, Jul 30, 2008
    #11
  12. Hongyu

    Hongyu Guest

    On Jul 29, 6:34 pm, Donkey Hot <-a-geek.com> wrote:
    > Hongyu <> wrote innews::
    >
    >
    >
    > > On Jul 29, 5:28 pm, Donkey Hot <-a-geek.com> wrote:
    > >> 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.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.
    Hongyu, Jul 30, 2008
    #12
  13. In article
    <>,
    Hongyu <> wrote:

    > On Jul 29, 5:58 pm, "John B. Matthews" <> wrote:
    > > In article
    > > <>,
    > >
    > >
    > >
    > > Hongyu <> wrote:
    > > > On Jul 29, 5:11 pm, Hongyu <> 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.

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

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
    John B. Matthews, Jul 30, 2008
    #13
  14. Hongyu

    Hongyu Guest

    On Jul 29, 9:54 pm, "John B. Matthews" <> wrote:
    > In article
    > <>,
    >
    >
    >
    >  Hongyu <> wrote:
    > > On Jul 29, 5:58 pm, "John B. Matthews" <> wrote:
    > > > In article
    > > > <>,

    >
    > > >  Hongyu <> wrote:
    > > > > On Jul 29, 5:11 pm, Hongyu <> 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.

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


    No problem and thanks for the help all the same.
    Hongyu, Jul 30, 2008
    #14
  15. Hongyu

    Dave Miller Guest

    Dave Miller, Jul 30, 2008
    #15
  16. Hongyu

    mail oo Guest

    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
    mail oo, Aug 6, 2008
    #16
  17. Hongyu

    mail oo Guest

    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
    mail oo, Aug 6, 2008
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vijay
    Replies:
    8
    Views:
    703
  2. Gary Wessle

    error running hello world

    Gary Wessle, Jul 19, 2006, in forum: C++
    Replies:
    3
    Views:
    273
    Thomas J. Gritzan
    Jul 19, 2006
  3. Roy
    Replies:
    6
    Views:
    609
    Roedy Green
    Jan 7, 2008
  4. JC

    Running Hello World function - security warning

    JC, Jan 15, 2009, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    676
  5. Larry
    Replies:
    27
    Views:
    424
    Michele Dondi
    Jan 25, 2005
Loading...

Share This Page