Код: Выделить всё
task bootup(type: JavaExec) {
dependsOn build
classpath = sourceSets.main.runtimeClasspath
main = 'org.example.ServerLauncher'
args 'hello'
maxHeapSize '512m'
}
Код: Выделить всё
package org.example;
import java.util.concurrent.CountDownLatch;
public class ServerLauncher {
private final CountDownLatch shutdownHook;
private final String[] args;
private ServerLauncher(String[] args) {
this.args = args;
this.shutdownHook = new CountDownLatch(1);
}
public static void main(String[] args) throws Exception {
new ServerLauncher(args).execute();
}
private void execute() throws Exception {
hangShutdownHook();
System.out.println("Launcher has started");
try {
shutdownHook.await();
} finally {
System.out.println("Launcher has closed");
}
}
private void hangShutdownHook() {
Thread thread = new Thread(this::triggerShutdown);
Runtime.getRuntime().addShutdownHook(thread);
}
private void triggerShutdown() {
System.out.println("Shutdown has triggered");
shutdownHook.countDown();
}
}
Код: Выделить всё
Launcher has started
^CShutdown has triggered
Launcher has closed
Код: Выделить всё
> Task :bootup
Launcher has started
91% EXECUTING [8s]
> :bootup
^C%
Подробнее здесь: https://stackoverflow.com/questions/641 ... forked-jvm