本文整理匯總了Java中com.sun.tools.attach.VirtualMachine類的典型用法代碼示例。如果您正苦於以下問題:Java VirtualMachine類的具體用法?Java VirtualMachine怎麽用?Java VirtualMachine使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
VirtualMachine類屬於com.sun.tools.attach包,在下文中一共展示了VirtualMachine類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: attach
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
/**
* Spawn recaf in the JVM by the given descriptor.
*
* @param vm
*/
public static void attach(VirtualMachineDescriptor vm) {
try {
// Run attach
VirtualMachine target = VirtualMachine.attach(vm);
String agentPath = getAgentJar();
if (!agentPath.endsWith(".jar")) {
Recaf.INSTANCE.logging.error(new RuntimeException("Recaf could not resolve a path to itself."));
return;
}
File agent = new File(agentPath);
if (agent.exists()) {
Recaf.INSTANCE.logging.info("Attempting to attach to '" + vm.displayName() + "' with agent '" + agent.getAbsolutePath() + "'.");
target.loadAgent(agent.getAbsolutePath());
target.detach();
} else {
Recaf.INSTANCE.logging.error(new RuntimeException("Recaf could not resolve a path to itself, attempt gave: " + agent.getAbsolutePath()));
}
} catch (Exception e) {
Recaf.INSTANCE.logging.error(e);
}
}
示例2: main
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
public static void main(String[] args) throws IOException,
AttachNotSupportedException, AgentLoadException,
AgentInitializationException, InterruptedException {
// 獲取當前jvm的進程pid
String pid = ManagementFactory.getRuntimeMXBean().getName();
int indexOf = pid.indexOf('@');
if (indexOf > 0) {
pid = pid.substring(0, indexOf);
}
System.out.println("當前JVM Process ID: " + pid);
// 獲取當前jvm
VirtualMachine vm = VirtualMachine.attach(pid);
// 當前jvm加載代理jar包,參數1是jar包路徑地址,參數2是給jar包代理類傳遞的參數
vm.loadAgent("D:/123.jar", "my agent:123.jar");
Thread.sleep(1000);
vm.detach();
}
示例3: getVmDescByServerName
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
/**
* 獲取指定服務名的本地JMX VM 描述對象
* @param serverName
* @return
*/
public static List<VirtualMachineDescriptor> getVmDescByServerName(String serverName){
List<VirtualMachineDescriptor> vmDescList = new ArrayList<>();
if (StringUtils.isEmpty(serverName)){
return vmDescList;
}
List<VirtualMachineDescriptor> vms = VirtualMachine.list();
for (VirtualMachineDescriptor desc : vms) {
//java -jar 形式啟動的Java應用
if(desc.displayName().matches(".*\\.jar(\\s*-*.*)*") && desc.displayName().contains(serverName)){
vmDescList.add(desc);
}else if(hasContainsServerName(desc.displayName(),serverName)){
vmDescList.add(desc);
}else if (isJSVC(desc.id(),serverName)){
VirtualMachineDescriptor descriptor = new VirtualMachineDescriptor(desc.provider(),desc.id(),serverName);
vmDescList.add(descriptor);
}
}
return vmDescList;
}
示例4: main
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
public static void main(String[] args) throws AttachNotSupportedException,
IOException, AgentLoadException, AgentInitializationException,
InterruptedException {
VirtualMachine vm=AgentUtil.getCurrentVm();
//先載入agent
vm.loadAgent("d:/springloaded-1.2.1.RELEASE.jar","myagent");
System.out.println("加載agent成功");
vm.detach();
TestSpringLoacedClass t1=new TestSpringLoacedClass();
for(int i=0;i<10000;i++)
{
TestSpringLoacedClass t2=new TestSpringLoacedClass();
t1.say2();
t2.say2();
Thread.sleep(1000);
}
}
示例5: dump
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
private static void dump(String pid, String options) throws IOException {
// parse the options to get the dump filename
String filename = parseDumpOptions(options);
if (filename == null) {
usage(1); // invalid options or no filename
}
// get the canonical path - important to avoid just passing
// a "heap.bin" and having the dump created in the target VM
// working directory rather than the directory where jmap
// is executed.
filename = new File(filename).getCanonicalPath();
// dump live objects only or not
boolean live = isDumpLiveObjects(options);
VirtualMachine vm = attach(pid);
System.out.println("Dumping heap to " + filename + " ...");
InputStream in = ((HotSpotVirtualMachine)vm).
dumpHeap((Object)filename,
(live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION));
drain(vm, in);
}
示例6: attach
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
private static VirtualMachine attach(String pid) {
try {
return VirtualMachine.attach(pid);
} catch (Exception x) {
String msg = x.getMessage();
if (msg != null) {
System.err.println(pid + ": " + msg);
} else {
x.printStackTrace();
}
if ((x instanceof AttachNotSupportedException) && haveSA()) {
System.err.println("The -F option can be used when the " +
"target process is not responding");
}
System.exit(1);
return null; // keep compiler happy
}
}
示例7: loadManagementAgent
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
private void loadManagementAgent() throws IOException {
VirtualMachine vm = null;
String name = String.valueOf(vmid);
try {
vm = VirtualMachine.attach(name);
} catch (AttachNotSupportedException x) {
IOException ioe = new IOException(x.getMessage());
ioe.initCause(x);
throw ioe;
}
vm.startLocalManagementAgent();
// get the connector address
Properties agentProps = vm.getAgentProperties();
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
vm.detach();
}
示例8: attachVirtualMachine
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
throws AttachNotSupportedException, IOException
{
if (vmd.provider() != this) {
throw new AttachNotSupportedException("provider mismatch");
}
// To avoid re-checking if the VM if attachable, we check if the descriptor
// is for a hotspot VM - these descriptors are created by the listVirtualMachines
// implementation which only returns a list of attachable VMs.
if (vmd instanceof HotSpotVirtualMachineDescriptor) {
assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
checkAttachPermission();
return new SolarisVirtualMachine(this, vmd.id());
} else {
return attachVirtualMachine(vmd.id());
}
}
示例9: attachVirtualMachine
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
throws AttachNotSupportedException, IOException
{
if (vmd.provider() != this) {
throw new AttachNotSupportedException("provider mismatch");
}
// To avoid re-checking if the VM if attachable, we check if the descriptor
// is for a hotspot VM - these descriptors are created by the listVirtualMachines
// implementation which only returns a list of attachable VMs.
if (vmd instanceof HotSpotVirtualMachineDescriptor) {
assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
checkAttachPermission();
return new LinuxVirtualMachine(this, vmd.id());
} else {
return attachVirtualMachine(vmd.id());
}
}
示例10: attachVirtualMachine
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
throws AttachNotSupportedException, IOException
{
if (vmd.provider() != this) {
throw new AttachNotSupportedException("provider mismatch");
}
// To avoid re-checking if the VM if attachable, we check if the descriptor
// is for a hotspot VM - these descriptors are created by the listVirtualMachines
// implementation which only returns a list of attachable VMs.
if (vmd instanceof HotSpotVirtualMachineDescriptor) {
assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
checkAttachPermission();
return new BsdVirtualMachine(this, vmd.id());
} else {
return attachVirtualMachine(vmd.id());
}
}
示例11: attachVirtualMachine
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
throws AttachNotSupportedException, IOException
{
if (vmd.provider() != this) {
throw new AttachNotSupportedException("provider mismatch");
}
// To avoid re-checking if the VM if attachable, we check if the descriptor
// is for a hotspot VM - these descriptors are created by the listVirtualMachines
// implementation which only returns a list of attachable VMs.
if (vmd instanceof HotSpotVirtualMachineDescriptor) {
assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
checkAttachPermission();
return new AixVirtualMachine(this, vmd.id());
} else {
return attachVirtualMachine(vmd.id());
}
}
示例12: testLocalAgent
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
public static void testLocalAgent(VirtualMachine vm) throws Exception {
Properties agentProps = vm.getAgentProperties();
String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
if (address != null) {
throw new Exception("Local management agent already started");
}
String result = vm.startLocalManagementAgent();
// try to parse the return value as a JMXServiceURL
new JMXServiceURL(result);
agentProps = vm.getAgentProperties();
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
if (address == null) {
throw new Exception("Local management agent could not be started");
}
}
示例13: main
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
public static void main(String args[]) throws Exception {
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
throw new RuntimeException("Test configuration error - no security manager set");
}
String pid = args[0];
boolean shouldFail = Boolean.parseBoolean(args[1]);
try {
VirtualMachine.attach(pid).detach();
if (shouldFail) {
throw new RuntimeException("SecurityException should be thrown");
}
System.out.println(" - attached to target VM as expected.");
} catch (Exception x) {
// AttachNotSupportedException thrown when no providers can be loaded
if (shouldFail && ((x instanceof AttachNotSupportedException) ||
(x instanceof SecurityException))) {
System.out.println(" - exception thrown as expected.");
} else {
throw x;
}
}
}
示例14: attach
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
public static void attach(Class<? extends Agent> agentClass, List<Class<?>> classes, File... libs) throws AgentInstantiationException, AttachNotSupportedException, AgentLoadException, AgentInitializationException, NotFoundException, CannotCompileException, IOException {
try {
Agent agent = agentClass.newInstance();
VirtualMachine vm;
switch (agent.type()) {
case MAIN:
vm = get1(agent.target());
break;
case PID:
vm = get2(agent.target());
break;
default:
vm = null;
break;
}
load(vm, agent, classes, libs);
} catch (IllegalAccessException | InstantiationException e) {
throw new AgentInstantiationException();
}
}
示例15: executeCommandForPid
import com.sun.tools.attach.VirtualMachine; //導入依賴的package包/類
private static void executeCommandForPid(String pid, String command, Object ... args)
throws AttachNotSupportedException, IOException,
UnsupportedEncodingException {
VirtualMachine vm = VirtualMachine.attach(pid);
// Cast to HotSpotVirtualMachine as this is an
// implementation specific method.
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
try (InputStream in = hvm.executeCommand(command, args)) {
// read to EOF and just print output
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
}
vm.detach();
}