I tried to compile a Hello world example in java and got a gcc error. As far as I understand it, gcc is trying to compile my java class (and for some reason failing to find the main method, but that's beside the point).

I have openSUSE 12.1 64 bit and JDK 1.7 (installed from Oracle's rpm). I have /usr/java/jdk1.7.0_04/bin/:/usr/java/jdk1.7.0_04/ in my $PATH. When I try to compile a java file, I get:

rumtscho@bradbury:/tmp> javac Hello.java
In function `_start':
/elf/start.S:109: undefined reference to `main'

Why is this happening and how can I correct the problem?

My complete $PATH is:

rumtscho@bradbury:/tmp> echo $PATH
  • I don't understand your question: is your problem that gcc is invoked in preference to some other java compiler? Or that gcc doesn't work? Does Hello.java have a main method? What is the exact value of your $PATH? Presumably the gcc javac is before the Oracle javac, or maybe javac is a configurable symlink (some distributions do this — I don't know about SuSE). – Gilles 'SO- stop being evil' Jun 1 '12 at 0:21
  • @Gilles Yes, I want the oracle compiler to be used when I call javac. For some reason, my $PATH seems doubled, I don't know if this is related. The application consists of nothing but a main method (which prints "Hello world"), so it is also a bit strange that gcc doesn't work. – rumtscho Jun 1 '12 at 0:28

Commands are looked up in $PATH in the order in which the directories are listed. In your case, it is likely from your description that there is no /home/rumtscho/bin/javac, no /usr/local/bin/javac, but a /usr/bin/javac that is the GCC Java compiler (or a symbolic link to it).

SuSE has an “alternatives” mechanism to handle programs that have multiple implementations. According to the SuSE documentation on installing Java, SuSE's Java packages use this mechanism. If the Oracle package respects this interface, then run

update-alternatives --config javac

to select your favorite Java compiler (you should do the same with java to select a matching runtime environment).

If the Oracle binary is not mentioned in the list, you can manually switch to it:

ln -snf /usr/java/jdk1.7.0_04/bin/{java,javac} /etc/alternatives/

Alternatively (but this is not recommended if the alternatives method works), you can create a symbolic link to your prefered javac in your ~/bin directory (per-user setting) or in /usr/local/bin (system-wide setting).

ln -s ../../java/jdk1.7.0_04/bin/javac /usr/local/bin

Another way would be to reorder your PATH to have the Oracle JDK directory first. In your ~/.profile, make sure to add /usr/java/jdk1.7.0_04/bin at the beginning of your PATH. You can take the opportunity to remove it if it was already in the $PATH, to avoid duplicate entries (which are harmless except for a very slight slowdown when looking for a command at the end of $PATH or looking up a non-existent command name).

prepend_to_path () {
  case "$PATH" in
    *":$1:"*) PATH="${PATH%%":$1:"*}:${PATH#*":$1:"}";;
prepend_to_path /usr/java/jdk1.7.0_04/bin
  • I just found the symlink under alternatives and changed it, and everything worked. I was busy writing this in an answer of my own when you posted your, much more complete answer. Thanks! – rumtscho Jun 1 '12 at 0:57
  • Although alternatives did not work for me (it came up with only one), it gave me the right direction. Fixing the symlink manually did the trick. – JensG Dec 21 '14 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.