How to start tomcat in Linux Ubuntu from Java or Scala program

I have one requirement where I need to start tomcat, nginx, kafka etc from scala or java program, I am able to check through java program that if they are running or not using ps-ef| grep name but if they are not running I want them to start itself from java or scala program, I tried this for tomcat

val pr = Runtime.getRuntime.exec(Array[String]("/home/administrator/Desktop/apache-tomcat-8.0.5/bin","-c","echo def123@| sudo -S startup.sh"))

but its not working, so can something plz help me out here!!!!

1 Answer

The main problem seems to be the usage of sudo. You need gksudo.


A simple Scala example

package com.askubuntu.users.aamir
import scala.sys.process._
object startProcess { def main(args: Array[String]) { Process("ls -la")! }
}

or with sudo-rights (you need gksudo)

package com.askubuntu.users.aamir
import scala.sys.process._
object startProcess { def main(args: Array[String]) { Process("gksudo nautilus")! }
}

and with a little bit more Java

package com.askubuntu.users.aamir
object startProcess { def main(args: Array[String]) { Runtime.getRuntime.exec(Array[String]("gksudo","nautilus")); }
}

And here is a Java method to start a process

package com.askubuntu.users.aamir;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class startProcess { public static void main(String[] args) { BufferedReader in; PrintWriter out = new PrintWriter(System.out); try { Process p = Runtime.getRuntime().exec(new String[]{"/home/administrator/Desktop/apache-tomcat-8.0.5/bin","-c","echo def123@| sudo -S startup.sh"}); in = new BufferedReader(new InputStreamReader(p.getInputStream())); String text; while ((text = in.readLine()) != null) { out.println(text); out.flush(); } } catch (IOException e) { e.printStackTrace(); } }
}

or

package com.askubuntu.users.aamir;
import java.io.IOException;
public class startProcess { public static void main(String[] args) { try { Runtime.getRuntime().exec(new String[] { "gksudo","nautilus", "/tmp" }); } catch (IOException e) { e.printStackTrace(); } }
}
7

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like