Discussion:
[arch-dev-public] JAVA_HOME in systemd
Guillaume ALAUX
2013-02-06 10:50:22 UTC
Permalink
Hi all,

I should have posted this a long time ago as suggested by Andy.

Basically I would like some advice from the systemd gurus on how to
provide a common place to set JAVA_HOME in systemd service files.

As you know all Java apps need JAVA_HOME to be set. But as systemd
service files do not inherit the shell's environment, all Java service
files will need to declare a
"Environment=JAVA_HOME=/lib/jvm/java-7-openjdk". Needless to say that
this is hardcoding the path and if one wants to change it for another
JVM, he/she will have to fix all service files.

Hence I was thinking about a common EnvironmentFile to hold this value
once and for all. I know these EnvironmentFile _should_ be avoided but
I think we are in a case where it could be tolerated. All Java service
file could then just refer to it throught
"EnvironmentFile=/the/path/to/this/file". Also it could be parsed by
/etc/profile.d/jre.sh to set the shell's environment.

So is there a simpler to do that in systemd? Or does this sound ok to
you? Also if it sounds OK, is there a standard place to put systemd
environment files like such?

Thanks

--
Guillaume
Thomas Bächler
2013-02-06 10:59:22 UTC
Permalink
Post by Guillaume ALAUX
Hence I was thinking about a common EnvironmentFile to hold this value
once and for all. I know these EnvironmentFile _should_ be avoided but
I think we are in a case where it could be tolerated. All Java service
file could then just refer to it throught
"EnvironmentFile=/the/path/to/this/file".
This seems like the only way. Such a file would however make all java
runtimes conflict, unless you find a very clever way around it.
Gaetan Bisson
2013-02-06 12:58:13 UTC
Permalink
Post by Thomas Bächler
Post by Guillaume ALAUX
Hence I was thinking about a common EnvironmentFile to hold this value
once and for all. I know these EnvironmentFile _should_ be avoided but
I think we are in a case where it could be tolerated. All Java service
file could then just refer to it throught
"EnvironmentFile=/the/path/to/this/file".
This seems like the only way. Such a file would however make all java
runtimes conflict, unless you find a very clever way around it.
You can always have each Java runtime provide a different file, and
include all of them in each Java service file using

EnvironmentFile=-/path/to/java/runtime/number/one
EnvironmentFile=-/path/to/java/runtime/number/two

etc.
--
Gaetan
Jan Steffens
2013-02-06 13:36:34 UTC
Permalink
Post by Gaetan Bisson
You can always have each Java runtime provide a different file, and
include all of them in each Java service file using
EnvironmentFile=-/path/to/java/runtime/number/one
EnvironmentFile=-/path/to/java/runtime/number/two
etc.
You can also pass a wildcard expression, avoiding hardcoding several files,
maybe like this:

EnvironmentFile=-/etc/java-runtime.d/*
EnvironmentFile=-/etc/java-runtime

Needs testing, but could allow the user to set a default runtime via symlink.

Alternatively, just EnvironmentFile=/etc/java-runtime and create this symlink
at post_install of every java-runtime, if it doesn't exist already.
To be tidy, post_remove then deletes the file if java-runtime.d
doesn't exist anymore.
Guillaume ALAUX
2013-02-06 14:08:13 UTC
Permalink
---------- Forwarded message ----------
From: Leonidas Spyropoulos <***@gmail.com>
Date: 6 February 2013 14:52
Subject: Re: [arch-dev-public] JAVA_HOME in systemd
Post by Jan Steffens
Post by Gaetan Bisson
You can always have each Java runtime provide a different file, and
include all of them in each Java service file using
EnvironmentFile=-/path/to/java/runtime/number/one
EnvironmentFile=-/path/to/java/runtime/number/two
etc.
You can also pass a wildcard expression, avoiding hardcoding several files,
EnvironmentFile=-/etc/java-runtime.d/*
EnvironmentFile=-/etc/java-runtime
Needs testing, but could allow the user to set a default runtime via symlink.
Alternatively, just EnvironmentFile=/etc/java-runtime and create this symlink
at post_install of every java-runtime, if it doesn't exist already.
To be tidy, post_remove then deletes the file if java-runtime.d
doesn't exist anymore.
I can't send to mailing list as I am not a dev / TU.
Isn't it possible to detect the JDK on runtime? Getting it from the
java command? (or the javac command)

--
Caution: breathing may be hazardous to your health.

#include <stdio.h>
int main(){printf("%s","\x4c\x65\x6f\x6e\x69\x64\x61\x73");}
Guillaume ALAUX
2013-02-06 14:08:53 UTC
Permalink
Post by Guillaume ALAUX
---------- Forwarded message ----------
Date: 6 February 2013 14:52
Subject: Re: [arch-dev-public] JAVA_HOME in systemd
Post by Jan Steffens
Post by Gaetan Bisson
You can always have each Java runtime provide a different file, and
include all of them in each Java service file using
EnvironmentFile=-/path/to/java/runtime/number/one
EnvironmentFile=-/path/to/java/runtime/number/two
etc.
You can also pass a wildcard expression, avoiding hardcoding several files,
EnvironmentFile=-/etc/java-runtime.d/*
EnvironmentFile=-/etc/java-runtime
Needs testing, but could allow the user to set a default runtime via symlink.
Alternatively, just EnvironmentFile=/etc/java-runtime and create this symlink
at post_install of every java-runtime, if it doesn't exist already.
To be tidy, post_remove then deletes the file if java-runtime.d
doesn't exist anymore.
I can't send to mailing list as I am not a dev / TU.
Isn't it possible to detect the JDK on runtime? Getting it from the
java command? (or the javac command)
--
Caution: breathing may be hazardous to your health.
#include <stdio.h>
int main(){printf("%s","\x4c\x65\x6f\x6e\x69\x64\x61\x73");}
The point is to enable the user to statically set which JRE must be
used. Detecting it at each java app runtime would not be ideal.

And I'd also rather not have to list all potential JRE in all java
systemd files.

Creating a symlink at post_install if it does not already exist looks
nice to me.
Tom Gundersen
2013-02-06 14:46:18 UTC
Permalink
Post by Guillaume ALAUX
Post by Guillaume ALAUX
---------- Forwarded message ----------
Date: 6 February 2013 14:52
Subject: Re: [arch-dev-public] JAVA_HOME in systemd
Post by Jan Steffens
Post by Gaetan Bisson
You can always have each Java runtime provide a different file, and
include all of them in each Java service file using
EnvironmentFile=-/path/to/java/runtime/number/one
EnvironmentFile=-/path/to/java/runtime/number/two
etc.
You can also pass a wildcard expression, avoiding hardcoding several files,
EnvironmentFile=-/etc/java-runtime.d/*
EnvironmentFile=-/etc/java-runtime
Needs testing, but could allow the user to set a default runtime via symlink.
Alternatively, just EnvironmentFile=/etc/java-runtime and create this symlink
at post_install of every java-runtime, if it doesn't exist already.
To be tidy, post_remove then deletes the file if java-runtime.d
doesn't exist anymore.
I can't send to mailing list as I am not a dev / TU.
Isn't it possible to detect the JDK on runtime? Getting it from the
java command? (or the javac command)
--
Caution: breathing may be hazardous to your health.
#include <stdio.h>
int main(){printf("%s","\x4c\x65\x6f\x6e\x69\x64\x61\x73");}
The point is to enable the user to statically set which JRE must be
used. Detecting it at each java app runtime would not be ideal.
And I'd also rather not have to list all potential JRE in all java
systemd files.
Creating a symlink at post_install if it does not already exist looks
nice to me.
The symlink seems reasonable to me. It would be nice if all the
distros/upstreams could agree on a scheme though as this does not sound
Arch/systemd specific.

Cheers,

Tom
Guillaume ALAUX
2013-02-07 11:33:53 UTC
Permalink
Post by Jan Steffens
Post by Guillaume ALAUX
Post by Guillaume ALAUX
---------- Forwarded message ----------
Date: 6 February 2013 14:52
Subject: Re: [arch-dev-public] JAVA_HOME in systemd
Post by Jan Steffens
Post by Gaetan Bisson
You can always have each Java runtime provide a different file, and
include all of them in each Java service file using
EnvironmentFile=-/path/to/java/runtime/number/one
EnvironmentFile=-/path/to/java/runtime/number/two
etc.
You can also pass a wildcard expression, avoiding hardcoding several
files,
Post by Guillaume ALAUX
Post by Guillaume ALAUX
Post by Jan Steffens
EnvironmentFile=-/etc/java-runtime.d/*
EnvironmentFile=-/etc/java-runtime
Needs testing, but could allow the user to set a default runtime via
symlink.
Post by Guillaume ALAUX
Post by Guillaume ALAUX
Post by Jan Steffens
Alternatively, just EnvironmentFile=/etc/java-runtime and create this
symlink
Post by Guillaume ALAUX
Post by Guillaume ALAUX
Post by Jan Steffens
at post_install of every java-runtime, if it doesn't exist already.
To be tidy, post_remove then deletes the file if java-runtime.d
doesn't exist anymore.
I can't send to mailing list as I am not a dev / TU.
Isn't it possible to detect the JDK on runtime? Getting it from the
java command? (or the javac command)
--
Caution: breathing may be hazardous to your health.
#include <stdio.h>
int main(){printf("%s","\x4c\x65\x6f\x6e\x69\x64\x61\x73");}
The point is to enable the user to statically set which JRE must be
used. Detecting it at each java app runtime would not be ideal.
And I'd also rather not have to list all potential JRE in all java
systemd files.
Creating a symlink at post_install if it does not already exist looks
nice to me.
The symlink seems reasonable to me. It would be nice if all the
distros/upstreams could agree on a scheme though as this does not sound
Arch/systemd specific.
Cheers,
Tom
Someone [0] proposed a clean/clever solution.

What about a symlink /usr/lib/jvm/default-java pointing to the
required runtime environment? Each Java runtime would need to check if
this link already exists. If not then just create it to point at the
newly installed JRE, if it already exists then just warn the user
about it and how to modify it.

Systemd service files could then use
'Environment=JAVA_HOME=/usr/lib/jvm/default-java' or if they wish use
a specific one.

[0] http://stackoverflow.com/a/663726

Loading...