Kill Java application started via run configuration

I'm executing a run configuration in a plugin I'm writing.

The Runc configuration, when getState method is called, does create an instance of MyStartRunState (see liosting above). The problem is that the jar runs an application on a port and if IntelliJ is quit without stopping the run configuration, the java application started from my jar continue to live (I checked that it runs on the port).

What should I do to ensure that the java application dies with IntelliJ?

public class MyStartRunState extends JavaCommandLineState{
private final Project project;
protected MyStartRunState(@NotNull final ExecutionEnvironment environment, @NotNull final Project) {
super(environment);
this.project = project;
}
@Override
protected JavaParameters createJavaParameters() throws ExecutionException {
final JavaParameters params = new JavaParameters();


final String jarPath = "user/bla/myjar-1.0.0.jar");

final int classPathType = JavaParameters.JDK_AND_CLASSES;
final String jreHome = //...;

JavaParametersUtil.configureProject(project, new JavaParameters(), classPathType, jreHome);
return params;
}
}
5 comments
Avatar
Vassiliy Kudryashov
Comment actions Permalink

The only hint I have for you is OSProcessHandler.killProcessTree(Process) but it should be used by default. Usually IDE stops all run configurations on exit, so your java application is 'too independant'. Do you start Java application directly by calling some main() method or maybe you use ProcessBuilder or Runtime.exec()?

0
Comment actions Permalink

Hi Vassily,

Yes, I use a main. I will try to switch to ProcessBuilder. Thanks for the hint.

0
Comment actions Permalink

Sorry, I think I misunderstood. What do you mean with starting the jar with the main?

I use the JavaCommandLineState class above with the args I specified. The jar should be started using the OSProcessHandler created in the startProcess() method inherited from JavaCommandLineState. What do you suggest to do here?

0
Avatar
Vassiliy Kudryashov
Comment actions Permalink

Does your JAR start a separate process? If so, try to add shutdown hook that stops this separate process.

0
Comment actions Permalink

Thanks for the tips.

I think it works as expected. It is like Tomcat at the end: if you kill IntelliJ the running appication server remains running.

Best
Michele

0

Please sign in to leave a comment.