albfan / mvnexec

bash script to find and execute java classes with main methods
18 stars 12 forks source link

Fix: set JAVA variable correctly if neither JAVA_HOME or JDK_HOME are set #8

Closed Pyxels closed 2 years ago

Pyxels commented 2 years ago

Previously this variable was set to nothing due to the removed part >&-, which resulted in an error when running, because the executable was empty.

albfan commented 2 years ago

What's your environment? That's probably a bashism to avoid any unexpected output, but working fine if you use bash.

Let me know to adapt script to other environments

Pyxels commented 2 years ago

I am using bash. The problem is that I have neither the JAVA_HOME or JDK_HOME environment variables set (maybe I should true), meaning that in the following code snippet, the third if is checked:

# exec {{{
if [ -n "$JDK_HOME" ]
then
   setJavaExecutable "$JDK_HOME"
elif [ -n "$JAVA_HOME" ]
then
   setJavaExecutable "$JAVA_HOME"
elif command -v java >&-
then
   JAVA=$(command -v java) #this line is changed, previously the same as above
else
   errorJavaExecutable
fi

This command command -v java >&- has not output though (but equates to true), so the JAVA variable would be set to an emtpy string, if not for the changed part.

Wit the change (running just command -v java) the variable JAVA is set to /usr/bin/java in my case, which is the desired behaviour.

Pyxels commented 2 years ago

command -v java >&- returns nothing command -v java returns the path, alternatively one could also use which java

albfan commented 2 years ago

I guess I was trying to ignore error output with 2>&- but cannot show any erroneous output with

command -v non-existing-command

So your PR is correct. Thanks!