I am working on an application created using the spring IO framework which displays the hostname using a json format.
The application is working fine when it is started from the command line by the command:
java -jar spring-cd.jar
which gives the required output:
but when it is run as a service using a systemd unit file the reuqired output(hostname) it is null:
the code for the unit file is the following:
[Unit]
After=network.target
[Service]
ExecStart=/usr/bin/java -jar /opt/training/spring-cd/spring-cd.jar
Type=simple
User=root
Restart=always
[Install]
WantedBy=default.target
I do not understand what is the difference between those approaches and why the second displays "null". Also i am fairly new and to systemd and linux.
Relevant Java code:
import java.lang.System;
public class Greeting {
private final String content;
public Greeting() {
this.content = getHostname();
}
private String getHostname(){
String hostname;
if (System.getProperty("os.name").startsWith("Windows")) {
hostname = System.getenv("COMPUTERNAME");
} else {
hostname = System.getenv("HOSTNAME");
}
return hostname;
}
public String getContent() {
return content;
}
}