简体   繁体   中英

Running a jar file from java code

I am trying to run a jar file from a java code but it does not work. I have tried the Runtime and th ProcessBuilder but it failed to start. the jar is the start.jar file for the Jetty Web Server

Any Help please ?

I don't know your use case but I think you are trying to start a Jetty server and in my humble opinion the best option for this situation is using Jetty as a dependency. Please put that jar file in your classpath and add these lines in your code.

import org.eclipse.jetty.server.Server;

public class App {
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        server.start();
        server.join();
    }
}

and from Runtime,

String directory = "[jetty directory]";
String format = "/usr/bin/java -Djetty.logging.dir=%s/logs -Djetty.home=%s -Djetty.base=%s -Djava.io.tmpdir=/tmp -jar %s/start.jar jetty.state=%s/jetty.state jetty-logging.xml jetty-started.xml";

Process process = Runtime.getRuntime()
                     .exec(String.format(format, directory, directory, directory, directory, directory));

process.waitFor();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM