java program

Hello everyone !

I installed jdk version :-

$ java -version
java version “1.6.0_0”
OpenJDK Runtime Environment (IcedTea6 1.6) (suse-5.9.4-i386)
OpenJDK Server VM (build 14.0-b16, mixed mode)

In directory :-

/home/<username>/program_files/jdk1.6.0_17

When I try to compile and run , it gives this :-

$ javac hello.java
/usr/lib/gcc/i586-suse-linux/4.4/…/…/…/crt1.o: In function _start': /usr/src/packages/BUILD/glibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115: undefined reference to main’
collect2: ld returned 1 exit status

And :-

$ java hello
Exception in thread “main” java.lang.NoClassDefFoundError: hello
Caused by: java.lang.ClassNotFoundException: hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: hello. Program will exit.

Whats the matter ?:expressionless:

Run these commands and report the output:

javac -version
which javac
rpm -qf $(which javac)
/usr/sbin/update-alternatives --display javac

@dk0505
You installed Sun JDK 1.6.0_17 in the directory
/home/<username>/program_files/jdk1.6.0_17

But, you already had OpenJDK (installed automatically by openSUSE).

When you typed commands like “java”, “javac” etc., it was invoking the OpenJDK commands, not your newly installed Sun JDK.

So, if you want to work with this JDK, you have 2 options:
(1) Set up Java environment to use this JDK
You need to set up PATH and JAVA_HOME

export PATH=/home/<username>/program_files/jdk1.6.0_17/bin:$PATH
export JAVA_HOME=/home/<username>/program_files/jdk1.6.0_17

You may put these in your .profile file.

(2) Install SunJDK and set it as the default JDK.
As root:

zypper in java-1_6_0-sun-devel
update-alternative -config javac

@syampillai

Worked out the first option. :slight_smile:

Thanks for your help !

@ken_yap

Thanks for posting !

The second option is actually the better one since it makes it apply across all users.

I suspect you actually got the GNU Java compiler the first time, from the look of the messages.

Are there dependency issues with changing the Java version globally to all users?

I noticed as well that I have a 1.4.2 version of Java installed by SUSE in my /usr directory. But in Home, I’m using the latest JDK. I’m using SUSE 10.1 for this computer.

I ask because other programs and applications, such as Open Office, etc probably depend on the version you have installed globally. In some cases I have had to manually change the Java version in either a configuration file (Netbeans) or in the Program options (OpenOffice).

I never uninstalled the old version of Java because I was afraid of creating issues with existing SUSE software that may be depending on it.

But I agree, it’s much better to have one version of Java installed if you can manage it.

You have to check if your other programs have installed their own libraries (generally files with “.jar” extensions). This is typically installed in “jre/lib/ext” (in the case of JDK) or “lib/ext” directory (in the case of JRE).

@ken_yap

Err…actually at present I just use linux for programming , so the first option will do for now !

I have c,c++ and java working now ! :smiley:

As soon as i get familier with linux , I will look into its details !

Of course. Just like with any other software that is version dependent. Especially as your installed version is 1.4 and currently Java is several versions ahead. But 10.1 did not come with recent JDK versions as RPMs so you would be installing in parallel anyway. So leave the official install alone and test the recent JDK with apps one at at time.

I’m getting the same error! :frowning:

cat Hello.java returns:


class Hello
{
        public static void main(String args])
        {
                System.out.println("Hello world");
        }
}

javac Hello.java returns:


/usr/lib/gcc/i586-suse-linux/4.4/../../../crt1.o: In function `_start':
/usr/src/packages/BUILD/glibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status

java -version returns:


java version "1.6.0_0"
OpenJDK Runtime Environment (IcedTea6 1.6) (suse-5.9.4-i386)
OpenJDK Client VM (build 14.0-b16, mixed mode)

javac --version returns:


javac (SUSE Linux) 4.4.1 [gcc-4_4-branch revision 150839]
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

which javac returns:


/usr/bin/javac

rpm -qf $(which javac) returns:


file /usr/bin/javac is not owned by any package

/usr/sbin/update-alternatives --display javac returns:


javac - status is auto.
 link currently points to /usr/lib/jvm/java-1.5.0-gcj/bin/javac
/usr/lib/jvm/java-1.5.0-gcj/bin/javac - priority 1500
 slave javah: /usr/lib/jvm/java-1.5.0-gcj/bin/javah
 slave rmic: /usr/lib/jvm/java-1.5.0-gcj/bin/rmic
 slave java_sdk: /usr/lib/jvm/java-1.5.0-gcj
 slave jar: /usr/lib/jvm/java-1.5.0-gcj/bin/jar
 slave javadoc: /usr/lib/jvm/java-1.5.0-gcj/bin/javadoc
 slave java_sdk_exports: /usr/lib/jvm-exports/java-1.5.0-gcj
Current `best' version is /usr/lib/jvm/java-1.5.0-gcj/bin/javac.

Please help! Thank you!

Actually, yesterday, I performed the following using YaST2:

  1. I checked the Java Development check-box in Patterns >> Development.
  2. I clicked Accept and it got installed.
  3. I once again started YaST2 and installed the jedit package and clicked Accept.
  4. I tried using jedit but didn’t like it.
  5. So I started YaST2 again and removed the jedit package.

Is the problem because of installing and removing jedit? Did it remove any required packages? YaST2 did not show me any package dependency popup windows.

The outputs shown in my above post are after performing these actions.

Forgot these:

echo $JAVA_BINDIR returns:


/usr/lib/jvm/java/bin

echo $JAVA_ROOT returns:


/usr/lib/jvm/java

echo $JDK_HOME returns:


/usr/lib/jvm/java

echo $PATH returns:


/home/rishabh/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/opt/kde3/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin:.

echo $JAVA_HOME returns:


/usr/lib/jvm/java

Are these environment variables properly set up?

Your program and environment are looking OK to me.
There could be some problem with the JDK you have installed. You can try the Sun’s JDK. Install it if you haven’t done it yet and switch to that.

To install Sun’s JDK:

zypper install java-1_6_0-sun-devel

To switch to this JDK:

update-alternatives -config javac

Okay, I got it working another way! Here’s what I did:

  1. Removed openJDK and all other related packages.
  2. (I have a Netbeans 6.7.1 DVD) so installed JDK from that to my $JAVA_HOME (/usr/lib/jvm/) directory.
  3. Ran the NetBeans installer.
  4. NetBeans detected JDK etc properly.
  5. Now it’s working awesome!

Actually installing java-1_6_0-sun-devel via the Internet was not feasible for me. Zypper said that it’ll download 30MB. My Internet plan is pay-as-you-use!

Got the whole idea when you said, “… install Sun’s JDK…” Thank you! :slight_smile: