Hi, I'm developing a Spring Boot application on Ubuntu (running inside a Vagrant-managed virtual machine). I'm following this official guide to run my app as a service. So I did:
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
(Of course I previously generated the jar with mvn clean package
and changed /var/myapp/myapp.jar
with the actual path for my jar)
It worked for a couple of days. Now when I do:
sudo service myapp start
...it says Unable to find Java
.
I can still run my app with java -jar myapp.jar
.
Here are some relevant settings from my bash profile:
export PATH=$PATH:~/java/bin:~/maven/bin
export JAVA_HOME=~/java
(Note: I installed both Java and Maven by downloading the official binaries into my home directory. Also, I shortened the full, detailed names, e.g. apache-maven-3.3.9-bin
-> maven
).
Also, this is the output from mvn -version
:
Maven home: /home/vagrant/maven
Java version: 1.8.0_77, vendor: Oracle Corporation
Java home: /home/vagrant/java/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-68-generic", arch: "amd64", family: "unix"
Maybe there's a bug here?
Comment From: wilkinsona
Here are some relevant settings from my bash profile:
It doesn't matter what's set in your bash profile, it's the profile of the user that will actually run the launch script that matters. As you're running the service with sudo
, that means that it's the superuser's bash profile that's of interest.
Maybe there's a bug here?
Given that you said it worked for a couple of days and then stopped working, I think it's unlikely that there's a bug in the script. It's more likely that something has changed on your machine.
The script looks in the following places for the java
executable:
- $JAVA_HOME/bin/java
- On the $PATH
- /usr/bin/java
java
needs to be available in one of these places for the superuser. My suspicion is that that is no longer the case.
Comment From: marcoliceti
You're probably right.
I'm searching for solutions to preserve my user's environment variables when using sudo. I tried:
sudo -E service myapp start
But I still get Unable to find Java
. A temporary workaround is to simply remove sudo
: I'm able to start and stop the service with just service myapp start
.
But since 99% this is not a Spring Boot bug, I'm closing the issue. Thanks for the quick and helpful response!
Comment From: marcoliceti
Ok, here is what's happening: service
only accepts LANG and TERM variables, so the -E
option is useless.
But it is useful when starting the service like this:
sudo -E /etc/init.d/recommender start
Comment From: webskyer
Link java to /sbin,will fix the error.
ln -s /usr/local/jdk/bin/java /sbin/java
:)
Comment From: marcoliceti
Hi. I ended up using supervisor to control my app (instead of running it as a service).
Comment From: ziggear
First, you can use following command to get java location:
where java
Then, link this java to '/usr/bin/java':
ln -s /path/to/java /usr/bin/java
see: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script and search "Unable to find Java"
Comment From: jcstier
I'm seeing this same problem... no matter how I symlink java.
root@my-app1:~# echo $JAVA_HOME
/opt/jdk1.8.0_111
root@my-app1:~# which java
/opt/jdk1.8.0_111/bin/java
root@my-app1:~# systemctl status my-test.service
● my-test.service - LSB: my-test
Loaded: loaded (/etc/init.d/my-test; bad; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2017-01-07 22:12:34 UTC; 23min ago
Docs: man:systemd-sysv-generator(8)
Process: 2100 ExecStart=/etc/init.d/my-test start (code=exited, status=1/FAILURE)
Jan 07 22:12:34 my-app1 systemd[1]: Starting LSB: my-test...
Jan 07 22:12:34 my-app1 my-test[2100]: Unable to find Java
Jan 07 22:12:34 my-app1 systemd[1]: my-test.service: Control process exited, code=exited status=1
Jan 07 22:12:34 my-app1 systemd[1]: Failed to start LSB: my-test.
Jan 07 22:12:34 my-app1 systemd[1]: my-test.service: Unit entered failed state.
Jan 07 22:12:34 my-app1 systemd[1]: my-test.service: Failed with result 'exit-code'.
I'm using:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
Comment From: jcstier
I got it working by running my service under a user with java env defined... not sure why I it wouldn't run as root.. it had the same java env. But probably better not to run as root anyhow :)
Comment From: aldoabril
In my case @webskyer's comments fix my problem
Comment From: ken-war
maybe you need use the jdk's rpm to install jdk on your machine
Comment From: jktheking
I used [my_application].conf file to configure "JAVA_HOME" and "JAVA_OPTS" . I started facing the issue "Java not found". I linked the java as mentioned by @webskyer "ln -s /usr/local/jdk/bin/java /sbin/java". This fixed the issue of "Java not found" but started facing invalid heap because of "Xmx" configured against "JAVA_OPTS".
Finally, I figured out this is the issue because of EOL character. I had created the the .conf file at windows machine and were using at centos. EOL for window is "CRLF" where as for unix it is "LF". When I replaced the EOL character to "LF" it started working.
How to replace EOL character using notepad++: Click on "Show All Characters" button : It shows the EOL character. To change the EOL character: go to edit --> EOL conversion--> Unix(LF)
Comment From: weizhangxiaohan
@jktheking thank you, your comments fix my problem.
Comment From: audiebant
Link java to /sbin,will fix the error.
ln -s /usr/local/jdk/bin/java /sbin/java
:)
that worked for me.
Comment From: drdro1
I am currently running as root and the solution:
ln -s /usr/local/jdk/bin/java /sbin/java
worked for me also. Just to state the obvious(or not) the command is:
ln -s /path/to/your/java /sbin/java
where /path/to/your/java is obtained by executing
which java
I think if you are running as root, you need to have /sbin/java in order to execute any Java executable with systemd. Source: http://www.linfo.org/sbin.html .
It's worth mentioning however that for security purposes it's not recommendable to use root to run/execute your services.