Re: Java Hello World running problem
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.