0

I am trying to modify /etc/init.d/jenkins script, in order to add my custom java path. My solutions is this:

JAVAPATH=$(type -p java) 
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$JAVAPATH

However, when I start the service and look into the logs, I see JAVAPATH variable is empty. On the other hand, if I write, for instance:

PYPATH=$(type -p python) 
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PYPATH

Then PATH variable will include python binary perfectly.

My final goal is removing the file name, just saving dirname so:

JAVAPATH=$(type -p java | xargs dirname)

On my custum scripts and tests, everything works fine, so I assume there is something with java that I don't know. I'm running a Debian 10 and my java path is /usr/lib/jvm/jdk-11.0.9/bin/java

Thanks in advance.

Regards

1
  • 1
    PATH should not contain paths to binaries (like java and python), it should contain paths to directories that the binaries are in. Furthermore, type -p will only find paths to binaries in directories that're already in PATH (i.e. those that don't need to be added because they're already there). Dec 6, 2020 at 8:15

1 Answer 1

0

type is a shell builtin that looks for a file in every directory specified in your $PATH variable.

In your example it's not showing anything because there's no java binary in any of the directories specified in your $PATH.

On the contrary, python is being found because it is indeed in the one of youyr $PATH directories (specifically in /usr/bin).

I don't know what installation method for Java you've used, but you could try to use /etc/alternatives/java as it is usually configured to point to the default java binary in your system.

Another not-so-clean option is to use dpkg -L <java package name> to list all the files provided by your jdk and filter them to find the binary you want to use:

$ JAVA_PATH=$(dpkg -L openjdk-11-jre-headless|fgrep bin/java)
$ echo $JAVA_PATH
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
1
  • After a deeper research, I've read init.d scripts should set it's own env variables for security reasons, so hardcoding path is it a good practice at all. Thanks for your response. Dec 6, 2020 at 12:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .