title: Start a Systemd service within a Java application
body: <p>I am writing a Java application which can run a systemd service.</p>
  
  <p>I have two systemd services (<strong>service1</strong> and <strong>service2</strong>). <strong>service1</strong> will start the Java application. Then inside the application it should start <strong>service2</strong>.
  I used the following statement to start <strong>service2</strong>:</p>
  
  <pre><code>Runtime.getRuntime().exec(new String[]{"sh", "-c", "systemctl start service2"});
  </code></pre>
  
  <p>But service2 is not started. (Command execution happens without any exceptions though) Any idea why?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I used <code>sudo systemctl start service1</code> to start the first service.</p>
  
  <p>Service 1 looks as follows.<br></p>
  
  <pre><code>[Unit]
  Description=Service 1
  
  [Service]
  WorkingDirectory=/usr/test
  Environment="DISPLAY=:0"
  User=las
  ExecStart=/usr/bin/java -jar test.jar
  
  [Install]
  WantedBy=graphical.target
  </code></pre>
  
  <p>Service 2 looks as follows</p>
  
  <pre><code>[Unit]
  Description=Service 2
  
  [Service]
  WorkingDirectory=/usr/test
  Environment="DISPLAY=:0"
  User=las
  ExecStart=/usr/bin/java -jar abc.jar
  
  [Install]
  WantedBy=graphical.target
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>After changing the code in a way to see the output stream of Runtime.getRuntime().exec() I got to know that it is returning the following error.<br></p>
  
  <pre><code>Failed to start service2.service: Interactive authentication required.
  </code></pre>