I have a very basic Spring Boot
application JAR
that exposes an API
.
All I want to do is start that JAR
programatically using the following code.
The problem is that I don't see any output or exceptions but JAR does not start. I tried changing the port to something that is not already bound but still won't work.
However, when I manually go into cmd
and cd
into C:\LDC\dev-server
directory and execute java -jar todo-rest-app-0.0.1-SNAPSHOT.jar --server.port=8081
the application starts and I am able to hit the API with http://localhost:8080/todos
url.
Main.java
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
new Main ().executeJar(new File("todo-rest-app-0.0.1-SNAPSHOT.jar"));
}
private void executeJar(File artifact) throws IOException, InterruptedException {
String jarName = "C:\\LDC\\dev-server\\" + artifact.getName();
System.out.println("jarName: " + jarName);
Process ps = Runtime.getRuntime().exec("java -jar " + jarName + " --server.port=8081");
ps.waitFor();
java.io.InputStream is = ps.getInputStream();
byte b[] = new byte[is.available()];
is.read(b, 0, b.length);
System.out.println(new String(b));
}
}
Output:
jarName: C:\LDC\dev-server\todo-rest-app-0.0.4-SNAPSHOT.jar
Process finished with exit code -1