Is it possible to get the process id of the java process that is currently running in the run dialog?

I'd like to attach a Java agent to the running Java project in the IDE dynamically. To do that I have to find the ID of that process, then:

VirtualMachine vm = VirtualMachine.attach(ID);

How to find the ID of the process programatically?

Or how to attach a dynamic agent to the running process(if there is a simpler solution)?

 

Edit: I've found this method:

Set<String> getAttachedPids(@NotNull Project project)

in

JavaDebuggerAttachUtil

but it gives me an empty set even if there is a java process running inside the IDE.

3 comments
Comment actions Permalink

Any help on this?

So I am developing a plugin for which I need the process id of the currently running Java program selected in the configuration menu.....

3
Comment actions Permalink

I'm having the same problem - I'd like to obtain a java process id of the Maven application that was previously started from the run configuration - is it possible?

 

0
Comment actions Permalink

Well, I was able to solve this partially, not sure if this is a right approach, but I can get hold of the process handler, e.g.

javaDebugProcess.getProcessHandler()

And then I use the following code:

String cmdLine = handler.toString();
ProcessInfo[] infoList = OSProcessUtil.getProcessList();
for (ProcessInfo info : infoList) {
if (info.getCommandLine().equals(cmdLine)) {
return String.valueOf(info.getPid());
}
}

It *kinda* works, but if I run a SpringBoot app and does not disable the forking, it doesn't work, because the PID of the Launcher is not the same as the PID of the forked SpringBoot app. So I'm back to square one and would really appreciate it if anyone can come up with a better solution.

 

0

Please sign in to leave a comment.