当前位置: 首页>>代码示例>>Java>>正文


Java VirtualMachine类代码示例

本文整理汇总了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);
	}
}
 
开发者ID:Col-E,项目名称:Recaf,代码行数:27,代码来源:Attach.java

示例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();
}
 
开发者ID:juebanlin,项目名称:util4j,代码行数:18,代码来源:AgentLoad.java

示例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;
}
 
开发者ID:DevopsJK,项目名称:SuitAgent,代码行数:25,代码来源:JMXUtil.java

示例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);
	}
}
 
开发者ID:juebanlin,项目名称:util4j,代码行数:18,代码来源:TestSpringLoaded.java

示例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);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:24,代码来源:JMap.java

示例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
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:19,代码来源:JMap.java

示例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();
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:20,代码来源:LocalVirtualMachine.java

示例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());
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:18,代码来源:SolarisAttachProvider.java

示例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());
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:18,代码来源:LinuxAttachProvider.java

示例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());
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:18,代码来源:BsdAttachProvider.java

示例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());
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:18,代码来源:AixAttachProvider.java

示例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");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:19,代码来源:StartManagementAgent.java

示例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;
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:26,代码来源:PermissionTest.java

示例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();
    }
}
 
开发者ID:ZeroMemes,项目名称:Agent-Lib,代码行数:21,代码来源:AgentLoader.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:JMap.java


注:本文中的com.sun.tools.attach.VirtualMachine类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。