Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../lib64/crt1.o: 
In function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.14.1/csu/../sysdeps/x86_64
/elf/start.S:109: undefined reference to `main'

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

Edit my complete $PATH is:

rumtscho@bradbury:/tmp> echo $PATH /home/rumtscho/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/usr/lib/mit/bin:.:/home/rumtscho/bin:/home/rumtscho/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/usr/lib/mit/bin:/home/rumtscho/android-sdk-linux/tools:/home/rumtscho/android-sdk-linux/platform-tools:/usr/java/jdk1.7.0_04/bin/:/usr/java/jdk1.7.0_04/:/home/rumtscho/bin:/home/rumtscho/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/usr/lib/mit/bin:/home/rumtscho/android-sdk-linux/tools:/home/rumtscho/android-sdk-linux/platform-tools:/usr/java/jdk1.7.0_04/bin/:/usr/java/jdk1.7.0_04/

share|improve this question
    
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 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

1 Answer 1

up vote 1 down vote accepted

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 () {
  PATH=":$PATH:"
  case "$PATH" in
    *":$1:"*) PATH="${PATH%%":$1:"*}:${PATH#*":$1:"}";;
  esac
  PATH="$1${PATH%:}"
}
prepend_to_path /usr/java/jdk1.7.0_04/bin
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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