05 May, 2010

Linux Java Hotspot/Gcj error

I got a really nasty Java error while trying to start a formerly working instance of glassfish v2.1.

I tried:
$ bin/asadmin create-domain
bin/asadmin: line 7: /home/h/run/gf21/lib/admin-cli.jar: Permission denied
Failed to load Main-Class manifest attribute from
bin/..//lib/admin-cli.jar

for some reason it didn't allow me to run such jar when previously was working like a charm! I just added execution permission to the jar file:

chmod +x lib/admin-cli.jar

this was all the problem with glassfish, but when solved the issue, this lend me to the next step...

$ bin/asadmin
invalid file (bad magic number): Exec format error
Failed to load Main-Class manifest attribute from
bin/..//lib/admin-cli.jar

OMG!! WTF??? this was just crazy. I googled it a bit and found out that the problem was my java version. I have uninstalled lots of times java-gcj and java-gcj-compat packages from my linux box (FC12), but everytime I update software it installs them again, therefore sometimes I just ignore I have it again in my system. This level passed! time to go to the next level!

Now that I uninstalled java-gcj again (with nodeps option otherwise I would uninstall openoffice), I tried again, now with glassfish installer. At the moment I was a bit demoralized by asadmin problem and wanted to isolate the real issue.

Then I ran:
$ /usr/java/latest/bin/java -Xmx256m -jar glassfish-installer-v2.1.1-b31g-linux.jar
Exception in thread "main" java.io.IOException: Cannot run program "java": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:466)
at glassfish.main(glassfish.java:97)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.(UNIXProcess.java:148)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 3 more

...that got me mad immediatly. What's this error?? I checked some forums and many of them said something about the path where you're executing the main class and so... This was not exactly my case because I got correctly the main class. In this forum they had a similar problem http://forums.java.net/jive/message.jspa?messageID=244886 so I just checked everything again.

The problem was solved with the simple command:
$ export PATH=$JAVA_HOME/bin:$PATH

previously I had correctly defined JAVA_HOME.

Conclusion

When java-gcj is installed, it adds a soft link in /usr/bin/java pointing to gcj java binaries. Therefore any program tries to execute java from there, but it doesn't corresponds to JAVA_HOME. When I removed gcj all my environment was still configured (not by me) to execute java from /usr/bin so it just didn't find JDK binaries. That's why just adding the proper java binary to the path, it worked again.

I have to complain to FC12 packagers because we all know that gcj it's not in a stable and fully compatible state, therefore it's very frustrating that you need to remove gcj and download/install Sun JDK. What's most frustrating it's that openoffice depends strongly on gcj and don't let you choose what JVM you wanna work with.

Supposedly Linux it's all about choice, and when sun jdk it's still not opensource, it's completely free tough, so it should be a way to FC12 packagers to let the users choose between JDK's.

Hope this helps anyone.