-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJoiner.java
50 lines (41 loc) · 1.81 KB
/
Joiner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import com.sun.tools.attach.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
public class Joiner {
/**
* tools.jar should be added into classpath
* @param args
*/
public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
attachToProcess(String.valueOf(previousJavaApp()));
}
private static void attachToProcess(String processId) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
// attach to target VM
VirtualMachine vm = VirtualMachine.attach(processId);
// get system properties in target VM
Properties props = vm.getSystemProperties();
// construct path to management agent
String home = props.getProperty("java.home");
String agent = home + File.separator + "lib" + File.separator + "management-agent.jar";
// load agent into target VM
vm.loadAgent(agent, "com.sun.management.jmxremote.port=5000,com.sun.management.jmxremote.authenticate=false,com.sun.management.jmxremote.ssl=false");
System.out.println("attached to "+processId);
System.in.read();
// detach
vm.detach();
}
private static int previousJavaApp() {
List<VirtualMachineDescriptor> vmList = VirtualMachine.list();
List<Integer> ids = new ArrayList<>();
for(VirtualMachineDescriptor vm : vmList){
ids.add(Integer.parseInt(vm.id()));
}
Collections.sort(ids, (o1, o2) -> o2.intValue()-o1.intValue());
System.out.println(" current process: "+ids.get(0)+" target process: "+ids.get(1));
return ids.get(1);
}
}