LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help needed to run simple java program in linux (https://www.linuxquestions.org/questions/linux-newbie-8/help-needed-to-run-simple-java-program-in-linux-806228/)

pinga123 05-05-2010 11:44 PM

Help needed to run simple java program in linux
 
Hi guys ,
This is the first time i m running java application inside linux.

i have installed jdk-6u20-linux-i586-rpm.bin jre-6u20-linux-i586-rpm.bin in my linux machine.

and set JAVA_HOME and JRE_HOME variables respectively.
Quote:

# echo $JAVA_HOME
/usr/java/jdk1.6.0_20/
# echo $JRE_HOME
/usr/java/jre1.6.0_20/
I have created a little java program helloworld.java
Quote:

class helloworld{
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
I m able to compile the program by doing javac helloworld.java
but not able to run the program by doing java helloworld.
It is giving me following error.
Quote:

Exception in thread "main" java.lang.NoClassDefFoundError: helloworld
at gnu.java.lang.MainThread.run(libgcj.so.7rh)
Caused by: java.lang.ClassNotFoundException: helloworld not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:/usr/java/jdk1.6.0_20/bin/], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at gnu.java.lang.MainThread.run(libgcj.so.7rh)
whenever i type java in command prompt it give me following output. M i missing anything.
Quote:

# java
Usage: gij [OPTION] ... CLASS [ARGS] ...
to invoke CLASS.main, or
gij -jar [OPTION] ... JARFILE [ARGS] ...
to execute a jar file
Try `gij --help' for more information.

pinga123 05-05-2010 11:55 PM

I think /usr/bin/java is not poited to /usr/java/jdk1.6.0_20/bin/java
It is pointed to some other java (which i m not sure).

How do i change this pointing?
If i do the changes will it have any serious impact ?

knudfl 05-06-2010 01:30 AM

Why not use a package meant for your OS ? EL5 or CentOS 5.x ?
(Please add info about the Linux used, for every question.!)

This package, jdk-1.6.0_20-fcs.i586.rpm is for all el5,
and was created by "CERN Scientific Linux"
http://ftp.scientificlinux.org/linux...0-fcs.i586.rpm
http://ftp.scientificlinux.org/linux/scientific/54

The command : 'java -version' will tell which setup you have.

The version can be changed with the commands : 1) su
2) /usr/sbin/update-alternatives --config java
(But the rpm package install will usually do the setup.)
..
EDIT /sbin/... to /usr/sbin/...

pinga123 05-06-2010 01:34 AM

Quote:

Originally Posted by knudfl (Post 3958886)
Why not use a package meant for your OS ? EL5 or CentOS 5.x ?
(Please add info about the Linux used, for every question.!)

This package, jdk-1.6.0_20-fcs.i586.rpm is for all el5,
and was created by "CERN Scientific Linux"
http://ftp.scientificlinux.org/linux...0-fcs.i586.rpm
http://ftp.scientificlinux.org/linux/scientific/54

The command : 'java -version' will tell which setup you have.

The version can be changed with the commands : 1) su
2) /sbin/update-alternatives --config java
(But the rpm package install will usually do the setup.)
..

My linux machine.
Quote:

LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: EnterpriseEnterpriseServer
Description: Enterprise Linux Enterprise Linux Server release 5.2 (Carthage)
Release: 5.2
Codename: Carthage

kirukan 05-06-2010 01:44 AM

Quote:

To compile
#javac helloworld.java

To run
#java helloworld
isn't it? No error return because i am using same jdk-1.6.0_20
and just setup JAVA_HOME path in /etc/profile as JAVA_HOME = /usr/java/jdk-1.6.0_20

pinga123 05-06-2010 05:00 AM

Quote:

Originally Posted by knudfl (Post 3958886)
Why not use a package meant for your OS ? EL5 or CentOS 5.x ?
(Please add info about the Linux used, for every question.!)

This package, jdk-1.6.0_20-fcs.i586.rpm is for all el5,
and was created by "CERN Scientific Linux"
http://ftp.scientificlinux.org/linux...0-fcs.i586.rpm
http://ftp.scientificlinux.org/linux/scientific/54

The command : 'java -version' will tell which setup you have.

The version can be changed with the commands : 1) su
2) /sbin/update-alternatives --config java
(But the rpm package install will usually do the setup.)
..

i dont have /sbin/update-alternatives what shall i do?

PMP 05-06-2010 05:40 AM

The error here is NoclassDefFound

Go to the directory where you helloworld.java is present.
execute
Code:

javac helloworld.java
If this is successful you will see a file called helloworld.class in the same directory

Now do

Code:

java --classpath . helloworld

knudfl 05-06-2010 06:25 AM

Sorry ... it's /usr/sbin/

... Continued in this parallel pinga123 thread ..
http://www.linuxquestions.org/questi...nd-jre-806269/

..


All times are GMT -5. The time now is 05:26 PM.