Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 758
  • Last Modified:

Cannot run Java HelloWorld program

I cannot run a java program on a Linux server, which has java installed. I can compile but cannot run the program. I have the error as below. Could you please suggest how to fix it? Thanks a lot.
$ which java
/usr/bin/java
$ java -version
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) 64-Bit Server VM (build 17.0-b16, mixed mode)
$cat HelloWorld.java
public class HelloWorld {

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

}
$javac HelloWorld.java
$ls HelloWorld.class
HelloWorld.class
$java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: HelloWorld.  Program will exit.
0
toooki
Asked:
toooki
  • 8
  • 8
  • 4
2 Solutions
 
duncanb7Commented:
What is your linux version ? you can get it from command of "uname -f"
What is the system CPU information ?  you can get it from  command of "cat /proc/cpuinfo"

It may be compatibility issue between your system and Java

Duncan
0
 
duncanb7Commented:
Try this,

java -classpath . HelloWorld

 that will work

Duncan
0
 
toookiAuthor Commented:
Thank you. I got these:

$uname -a
Linux snapfish.myco.com 2.6.18-194.26.1.el5 #1 SMP Tue Nov 9 12:54:20 EST 2010 x86_64 x86_64 x86_64 GNU/Linux

$cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           L5420  @ 2.50GHz
stepping        : 6
cpu MHz         : 2003.000
cache size      : 6144 KB
physical id     : 1
siblings        : 4
core id         : 0
cpu cores       : 4
apicid          : 4
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr sse4_1 lahf_lm
bogomips        : 5000.17
clflush size    : 64
cache_alignment : 64
address sizes   : 38 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           L5420  @ 2.50GHz
stepping        : 10
cpu MHz         : 2003.000
cache size      : 6144 KB
physical id     : 0
siblings        : 4
core id         : 0
cpu cores       : 4
apicid          : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr sse4_1 lahf_lm
bogomips        : 5000.18
clflush size    : 64
cache_alignment : 64
address sizes   : 38 bits physical, 48 bits virtual
power management:

processor       : 2
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           L5420  @ 2.50GHz
stepping        : 6
cpu MHz         : 2003.000
cache size      : 6144 KB
physical id     : 1
siblings        : 4
core id         : 1
cpu cores       : 4
apicid          : 5
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr sse4_1 lahf_lm
bogomips        : 5000.12
clflush size    : 64
cache_alignment : 64
address sizes   : 38 bits physical, 48 bits virtual
power management:

processor       : 3
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           L5420  @ 2.50GHz
stepping        : 10
cpu MHz         : 2003.000
cache size      : 6144 KB
physical id     : 0
siblings        : 4
core id         : 1
cpu cores       : 4
apicid          : 1
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr sse4_1 lahf_lm
bogomips        : 5000.13
clflush size    : 64
cache_alignment : 64
address sizes   : 38 bits physical, 48 bits virtual
power management:

processor       : 4
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           L5420  @ 2.50GHz
stepping        : 6
cpu MHz         : 2003.000
cache size      : 6144 KB
physical id     : 1
siblings        : 4
core id         : 2
cpu cores       : 4
apicid          : 6
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr sse4_1 lahf_lm
bogomips        : 5000.10
clflush size    : 64
cache_alignment : 64
address sizes   : 38 bits physical, 48 bits virtual
power management:

processor       : 5
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           L5420  @ 2.50GHz
stepping        : 10
cpu MHz         : 2003.000
cache size      : 6144 KB
physical id     : 0
siblings        : 4
core id         : 2
cpu cores       : 4
apicid          : 2
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr sse4_1 lahf_lm
bogomips        : 5000.20
clflush size    : 64
cache_alignment : 64
address sizes   : 38 bits physical, 48 bits virtual
power management:

processor       : 6
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           L5420  @ 2.50GHz
stepping        : 6
cpu MHz         : 2003.000
cache size      : 6144 KB
physical id     : 1
siblings        : 4
core id         : 3
cpu cores       : 4
apicid          : 7
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr sse4_1 lahf_lm
bogomips        : 5000.32
clflush size    : 64
cache_alignment : 64
address sizes   : 38 bits physical, 48 bits virtual
power management:

processor       : 7
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           L5420  @ 2.50GHz
stepping        : 10
cpu MHz         : 2003.000
cache size      : 6144 KB
physical id     : 0
siblings        : 4
core id         : 3
cpu cores       : 4
apicid          : 3
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr sse4_1 lahf_lm
bogomips        : 5000.27
clflush size    : 64
cache_alignment : 64
address sizes   : 38 bits physical, 48 bits virtual
power management:
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
duncanb7Commented:
Try this,

java -classpath . HelloWorld

 that will work


And if possible,  run command of "more /etc/*-release" to check your linux Distribution
Java just support this for Linux :


Linux
Oracle Linux 5.5+
Oracle Linux 6.x (32-bit), 6.x (64-bit)*
Red Hat Enterprise Linux 5.5+, 6.x (32-bit), 6.x (64-bit)*
Ubuntu Linux 10.04 and above
Suse Linux Enterprise Server 10 SP2, 11.x

Open in new window

Duncan
0
 
toookiAuthor Commented:
Thank you.

$more /etc/*-release
CentOS release 5.5 (Final)

I tried:
$java -classpath . HelloWorld

And it worked. Thank you.

I actually simplified my question. Although HelloWorld worked, my actual code is still giving the same problem:
I tried:
$CLASSPATH=$(ls $HIVE_HOME/lib/hive-serde-*.jar):$(ls $HIVE_HOME/lib/hive-exec-*.jar):$(ls $HADOOP_HOME/hadoop-core-*.jar)
$javac -cp $CLASSPATH PutMerge.java
$java -classpath . PutMerge
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FileSystem
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.fs.FileSystem
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: PutMerge.  Program will exit.


Could you please suggest why it is not running. Compilation worked without errors.
Thanks a lot.
0
 
toookiAuthor Commented:
PutMerge.java code:
$cat PutMerge.java
public class PutMerge {

public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(conf);
FileSystem local = FileSystem.getLocal(conf);

Path inputDir = new Path(args[0]);
Path hdfsFile = new Path(args[1]);

try {
FileStatus[] inputFiles = local.listStatus(inputDir);
FSDataOutputStream out = hdfs.create(hdfsFile);

for (int i=0; i<inputFiles.length; i++) {
System.out.println(inputFiles[i].getPath().getName());
FSDataInputStream in = local.open(inputFiles[i].getPath());
byte buffer[] = new byte[256];
int bytesRead = 0;
while( (bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
in.close();
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Open in new window

0
 
duncanb7Commented:
The hadoop-common-*.jar has to be included for the org.apache.commons.configuration.Configuration class

duncan
0
 
CEHJCommented:
$java -classpath . PutMerge

Open in new window

completely replaces the classpath you've already used. Since the current directory was not in that classpath (it probably should have been), you actually need

java -classpath .:${CLASSPATH} PutMerge

Open in new window

0
 
toookiAuthor Commented:
Thank you but I still get the error.

I compileted with the command:
$CLASSPATH=$(ls $HIVE_HOME/lib/hive-serde-*.jar):$(ls $HIVE_HOME/lib/hive-exec-*.jar):$(ls $HADOOP_HOME/hadoop-core-*.jar)
$javac -cp $CLASSPATH PutMerge.java
$ls PutMerge.class
PutMerge.class

Now I tried:
$CLASSPATH=$(ls $HIVE_HOME/lib/hive-serde-*.jar):$(ls $HIVE_HOME/lib/hive-exec-*.jar):$(ls $HADOOP_HOME/hadoop-core-*.jar)
$echo $CLASSPATH
/usr/lib/hive/lib/hive-serde-0.7.1-cdh3u4.jar:/usr/lib/hive/lib/hive-exec-0.7.1-cdh3u4.jar:/usr/lib/hadoop/hadoop-core-0.20.2-cdh3u4.jar
$java -classpath .:${CLASSPATH} PutMerge
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FileSystem
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.fs.FileSystem
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: PutMerge.  Program will exit.

Open in new window


If I try:
 
$java -cp $($HADOOP_HOME/bin/hadoop classpath):. PutMerge
java -cp $($HADOOP_HOME/bin/hadoop classpath):. PutMerge
14/07/28 16:33:40 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
14/07/28 16:33:40 INFO security.JniBasedUnixGroupsMapping: Bailing out since native library couldn't be loaded
Exception in thread "main" java.lang.ExceptionInInitializerError
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:951)
        at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:1002)
        at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:1028)
        at org.apache.hadoop.security.Groups.<init>(Groups.java:48)
        at org.apache.hadoop.security.Groups.getUserToGroupsMappingService(Groups.java:137)
        at org.apache.hadoop.security.UserGroupInformation.initialize(UserGroupInformation.java:251)
        at org.apache.hadoop.security.UserGroupInformation.ensureInitialized(UserGroupInformation.java:229)
        at org.apache.hadoop.security.UserGroupInformation.isSecurityEnabled(UserGroupInformation.java:307)
        at org.apache.hadoop.security.UserGroupInformation.getLoginUser(UserGroupInformation.java:536)
        at org.apache.hadoop.security.UserGroupInformation.getCurrentUser(UserGroupInformation.java:519)
        at org.apache.hadoop.fs.FileSystem$Cache$Key.<init>(FileSystem.java:1717)
        at org.apache.hadoop.fs.FileSystem$Cache$Key.<init>(FileSystem.java:1710)
        at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:1578)
        at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:228)
        at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:111)
        at PutMerge.main(PutMerge.java:12)
Caused by: java.lang.RuntimeException
        at org.apache.hadoop.security.JniBasedUnixGroupsMapping.<clinit>(JniBasedUnixGroupsMapping.java:44)
        ... 18 more

Open in new window


Now if I try:
 
$CLASSPATH=$(ls $HIVE_HOME/lib/hive-serde-*.jar):$(ls $HIVE_HOME/lib/hive-exec-*.jar):$(ls $HADOOP_HOME/hadoop-core-*.jar)
$java -cp $($CLASSPATH classpath):. PutMerge
-bash: /usr/lib/hive/lib/hive-serde-0.7.1-cdh3u4.jar:/usr/lib/hive/lib/hive-exec-0.7.1-cdh3u4.jar:/usr/lib/hadoop/hadoop-core-0.20.2-cdh3u4.jar: No such file or directory
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FileSystem
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.fs.FileSystem
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: PutMerge.  Program will exit.

Open in new window

0
 
duncanb7Commented:
I don't see you include $HADOOP_HOME/hadoop-common-*.jar
0
 
toookiAuthor Commented:
Thank you.

I do not see hadoop-common-*.jar file under $HADOOP_HOME. (I attached the screenshot)

I tried:
$CLASSPATH=$(ls $HIVE_HOME/lib/hive-serde-*.jar):$(ls $HIVE_HOME/lib/hive-exec-*.jar):$(ls $HADOOP_HOME/hadoop*.jar)
$echo $CLASSPATH
/usr/lib/hive/lib/hive-serde-0.7.1-cdh3u4.jar:/usr/lib/hive/lib/hive-exec-0.7.1-cdh3u4.jar:/usr/lib/hadoop/hadoop-0.20.2-cdh3u4-ant.jar /usr/lib/hadoop/hadoop-0.20.2-cdh3u4-core.jar /usr/lib/hadoop/hadoop-0.20.2-cdh3u4-examples.jar /usr/lib/hadoop/hadoop-0.20.2-cdh3u4-test.jar /usr/lib/hadoop/hadoop-0.20.2-cdh3u4-tools.jar /usr/lib/hadoop/hadoop-ant-0.20.2-cdh3u4.jar /usr/lib/hadoop/hadoop-ant.jar /usr/lib/hadoop/hadoop-core-0.20.2-cdh3u4.jar /usr/lib/hadoop/hadoop-core.jar /usr/lib/hadoop/hadoop-examples-0.20.2-cdh3u4.jar /usr/lib/hadoop/hadoop-examples.jar /usr/lib/hadoop/hadoop-test-0.20.2-cdh3u4.jar /usr/lib/hadoop/hadoop-test.jar /usr/lib/hadoop/hadoop-tools-0.20.2-cdh3u4.jar /usr/lib/hadoop/hadoop-tools.jar
java -classpath .:${CLASSPATH} PutMerge
Exception in thread "main" java.lang.NoClassDefFoundError: /usr/lib/hadoop/hadoop-0/20/2-cdh3u4-core/jar
Caused by: java.lang.ClassNotFoundException: .usr.lib.hadoop.hadoop-0.20.2-cdh3u4-core.jar
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: /usr/lib/hadoop/hadoop-0.20.2-cdh3u4-core.jar.  Program will exit.

hadoop_home
0
 
duncanb7Commented:
I am not quite sure where it is, could you use linux command to search it ?

it is at I guess only
 $HADOOP_HOME/share/hadoop/common/hadoop-common-2.2.0.jar
0
 
duncanb7Commented:
Why not reinstall apache hadoop and search its site from google ?

Duncan
0
 
CEHJCommented:
Try

java -cp '.:/usr/lib/hive/lib/*:/usr/lib/hadoop/*' PutMerge

Open in new window

0
 
toookiAuthor Commented:
Thank you.
I searched for the jar files and all the files are there under $HADOOP_HOME directory. I have attached the output screenshot of the ls -l on that directory.
It is hard for me to reinstall apache hadoop there as it is running the hadoop production server.

I tried the above and got the small error:
$ java -cp '.:/usr/lib/hive/lib/*:/usr/lib/hadoop/*' PutMerge
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at PutMerge.main(PutMerge.java:15)
$
hadpp_home
0
 
duncanb7Commented:
PutMerge that is your writing code ? it seems it is code issue

Take a look this have similar error, ArrayIndexOutOfBoundsException,and its tutorial at this site
http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-arrayindexoutofboundsexception-how-to-handle-array-index-out-of-bounds-exception/

Duncan
0
 
toookiAuthor Commented:
Thank you. Yes it is a code issue that I will debug and fix.

The compilation works!!! Many thanks ..

java -cp '.:/usr/lib/hive/lib/*:/usr/lib/hadoop/*' PutMerge
The above worked, The syntax is hard to me but it worked..which is great, So I think the hadoop libraries that I have is good to compile..

Thank you.
0
 
CEHJCommented:
I tried the above and got the small error:
That means PutMerge is expecting at least one argument to be passed to the app
0
 
toookiAuthor Commented:
Thank you. I later realized that.
I anyway recovered from the most problematic part (compile and run).
Thanks a lot.
0
 
CEHJCommented:
:)

fyi, where you have reasonably complex classpaths and you don't want/need the overhead of an IDE, get used to using Ant at the command line - it's lightweight and powerful
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now