Java binary locator


Forum: Other Help Topics
Topic: Java binary locator
started by: Zucca

Posted by Zucca on Oct. 18 2006,15:02
I'm planning a bash script for DSL that checks if java binary is located somewhere in the filesystem. If found then make an alias like this: alias java="/opt/javasomething/bin/java" or make a symlink o to $PATH or even better and easier: scrpt itself is in $PATH and if no java is installed it asks if user wants to install java. If yes then it installs java and then executes the original command. Ofcourse if java executable is already found then just run java executable with arguments.

Why this?
Because it is very easy to run java programs in linux only if java is installed. Just as simple as: $ java -jar your_program.jar
And with this script people new to Linux can easily download Java programs and run them in DSL.

I'd like to have some tips about  how this script should look for the java executable.
Also opinions about how this script should act are welcome. =)

Posted by mikshaw on Oct. 18 2006,16:57
Look at /usr/local/bin/links for a simple way to do part of it in lua.
This script does not handle files not in PATH, however.

I'm not sure how it would be done in Lua, but something like this might work in Bash:
(untested)
Code Sample
#!/bin/sh
if which java &>/dev/null; then
 JAVA=`which java`
elif [ -x "/opt/javasomething/bin/java" ]; then
 JAVA="/opt/javasomething/bin/java"
else
  echo "retrieving jre1_5_0.uci ..."
  mydsl-load jre1_5_0.uci uci && JAVA="/opt/javasomething/bin/java"
fi

if [ -x "${JAVA}" ]; then
 exec ${JAVA} "$@"
else
 echo "error!"
 exit 1
fi

Use this in place of the java command...but don't name it "java" unless it's not in your PATH.

Posted by Zucca on Oct. 18 2006,17:29
Many thanks. I'll see what I can do. :)
Posted by roberts on Oct. 18 2006,18:38
Don't forget CLASSPATH and some Java stuff wants JAVA_HOME.
Posted by Zucca on Oct. 18 2006,19:00
I managed to find java binary location like this:
Code Sample
JAVAEXEC=`sudo find /* | grep "/bin/java\$" 2> /dev/null `; echo $JAVAEXEC


At least it works with DSL. =D And that is what I actually need.

Next thing is to add that into script which checks if it is an executable:
Code Sample
if [ -x $JAVAEXEC ]

But I think I should also make it to check if it is a symlink...

Posted by ^thehatsrule^ on Oct. 18 2006,20:33
This is just my opinion:
using find in / is not a good idea, since that will go through every possible directory (including attached media).  At the very least, start in /opt (and/or /usr).

Also, what if there is more than one instance of /bin/java ?  Then there'd be multiple entries.

I'd suggest something like...
Code Sample
for eachdir in `ls -d /opt/java* 2>/dev/null`; do
 for TEMP in `find "$eachdir" 2>/dev/null | grep -re "/bin/java$" `; do
   if [ -x "$TEMP" ]; then
     JAVAEXEC="$TEMP"
     break 2;
   fi
 done
done


Also, why would you need to check if it is a symlink instead?

Posted by Zucca on Oct. 19 2006,09:55
Quote (^thehatsrule^ @ Oct. 18 2006,18:33)
This is just my opinion:
using find in / is not a good idea, since that will go through every possible directory (including attached media).  At the very least, start in /opt (and/or /usr).

Also, what if there is more than one instance of /bin/java ?  Then there'd be multiple entries.

I'd suggest something like...
Code Sample
for eachdir in `ls -d /opt/java* 2>/dev/null`; do
 for TEMP in `find "$eachdir" 2>/dev/null | grep -re "/bin/java$" `; do
   if [ -x "$TEMP" ]; then
     JAVAEXEC="$TEMP"
     break 2;
   fi
 done
done


Also, why would you need to check if it is a symlink instead?

I tought that find /* thing too. And it fastens process much by skipping /mnt.

And if there's more tha one java binary file I have to make script ask for which to use.

Looking for symlink is because if Java has been installed by some other way (to an onther directory)  and if that install has created that symlink there as to act like normal install.

Posted by mikshaw on Oct. 19 2006,13:17
It's kinda wasteful to find anywhere other than /opt, in my opinion.  If Java has been installed via source or debian/rpm package, it will almost 100% definitely without doubt be in PATH, probably for sure.  If not, the user already knows enough about the system to not need a failsafe script, or else he's already got bigger problems to deal with.
The only place where finding it would be necessary is for tar.gz or uci myDSL extensions, and since you already know where those install, you can narrow your search dramatically.

I think you're making this project much more complicated than it needs to be. How many variations of Java can there possibly be, especially for DSL? The DSL user either 1) knows enough to be able to easily deal with PATH issues, or 2) isn't going to install Java by any method other than MyDSL or Deb.  Maybe I'm overlooking custom installers in an official release (like Firefox does)?  I don't know if that applies to Java.

Posted by Zucca on Oct. 19 2006,20:41
I just want to make few things more simple. =) That's all.
Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.