How to use VirtualMachine class of com.sun.tools.attach package

Best Powermock code snippet using com.sun.tools.attach.VirtualMachine

Source:MyAgent.java Github

copy

Full Screen

1package agent;2import com.sachin.agent.transformer.GreetingTransformer;3import com.sun.tools.attach.VirtualMachine;4import java.lang.instrument.Instrumentation;5@SuppressWarnings("AlibabaRemoveCommentedCode")6public class MyAgent {7 /**8 * Java Agent支持目标JVM启动时加载,也支持在目标JVM运行时加载,这两种不同的加载模式会使用不同的入口函数,如果需要在目标JVM启动的同时加载Agent,9 * 那么可以选择实现下面的方法:10 * [1] public static void premain(String agentArgs, Instrumentation inst);11 * [2] public static void premain(String agentArgs);12 *13 *14 *15 *16 * 启动时加载 agent,-javaagent 传入的 jar 包需要在 MANIFEST.MF 中包含 Premain-Class 属性,17 * 此属性的值是 代理类 的名称,并且这个 代理类 要实现 premain 静态方法。18 *19 *20 * Agent 是在 Java 虚拟机启动之时加载的,这个加载处于虚拟机初始化的早期,在这个时间点上:21 * 所有的 Java 类都未被初始化;22 * 所有的对象实例都未被创建;23 * 因而,没有任何 Java 代码被执行;24 *25 *26 * java -javaagent:/Users/jingrzhang/sourceCodes/JavaAgent/first-agent/target/my-agent.jar=abc -jar app-demo-1.0-SNAPSHOT.jar27 *28 */29 public static void premain(String agentArgs, Instrumentation inst) {30 System.out.println("MyAgent#premain");31 System.out.println("args:" + agentArgs + "\n");32 inst.addTransformer(new GreetingTransformer(agentArgs),true);33 }34 /**35 * JDK1.6新增了attach方式,可以对运行中的java进程附加agent,提供了动态修改运行中已经被加载的类的途径。36 * 一般通过VirtualMachine的attach(pid)方法获得VirtualMachine实例,随后可调用loadagent方法将JavaAgent的jar包加载到目标JVM中。37 *38 * 在进程B中向进程A中注入java agent,需要满足以下条件:39 * 进程B的classpath中必须有tools.jar(提供VirtualMachine attach api),jdk默认有tools.jar,jre默认没有。40 *41 * tools.jar 这个包位于jdk的lib目录下, IDEA中安装的java 依赖是 JRE,也就是jdk安装目录下的jre/lib 目录42 *43 * 而这个jre/lib 目录下 没有 jdk目录下的tools.jar44 *45 *而这里用到的VirtualMachine是就是 tools.jar 包中的类。46 * import com.sun.tools.attach.VirtualMachine;47 * import com.sun.tools.attach.VirtualMachineDescriptor;48 *49 * 在jdk1.8中 tools.jar 包中没有了 VirtualMachine, 而是存在50 * sun.tools.attach.HotSpotVirtualMachine51 *52 * com.sun.tools.attach.VirtualMachine53 *54 */55 public static void loadVirtualMachine() throws Exception{56 String jvmPid="targetPid";57// VirtualMachine jvm = VirtualMachine.attach(jvmPid);58// jvm.loadAgent(agentFilePath);//agentFilePath为agent的路径59// jvm.detach();60// logger.info("Attached to target JVM and loaded Java agent successfully");61 //实际返回的是 hotspotVirtualMachine 实现类62 VirtualMachine virtualMachine = VirtualMachine.attach(jvmPid);63 virtualMachine.loadAgent("agentFilePath");64 virtualMachine.detach();65 }66}

Full Screen

Full Screen

Source:AttachToolMain.java Github

copy

Full Screen

...56import com.sun.tools.attach.AgentInitializationException;7import com.sun.tools.attach.AgentLoadException;8import com.sun.tools.attach.AttachNotSupportedException;9import com.sun.tools.attach.VirtualMachine;10import com.sun.tools.attach.VirtualMachineDescriptor;1112/**13 * Add tools.jar14 * @author geym15 *16 */17public class AttachToolMain {18 public static void main(String[] args) throws AttachNotSupportedException, IOException, AgentLoadException, AgentInitializationException {19 List<VirtualMachineDescriptor> list = VirtualMachine.list(); 20 for (VirtualMachineDescriptor vmd : list) 21 { 22 if(vmd.displayName().endsWith("RunLoopAccountMain")){ 23 VirtualMachine virtualmachine = VirtualMachine.attach(vmd.id()); 24 virtualmachine.loadAgent("D:\\ja.jar", "argument for agent");25 System.out.println("ok");26 virtualmachine.detach();27 }28 } 29 }30} ...

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.*;2import java.io.IOException;3import java.util.List;4public class 4 {5 public static void main(String[] args) throws IOException {6 List<VirtualMachineDescriptor> list = VirtualMachine.list();7 for (VirtualMachineDescriptor vmd : list) {8 System.out.println(vmd.id() + " " + vmd.displayName());9 }10 }11}

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.*;2public class 4 {3 public static void main(String[] args) throws Exception {4 VirtualMachine vm = VirtualMachine.attach("1234");5 vm.loadAgent("myagent.jar");6 vm.detach();7 }8}9import com.sun.tools.attach.*;10import java.io.*;11public class myagent {12 public static void agentmain(String agentArgs, Instrumentation inst) {13 System.out.println("Agent is running");14 }15}

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1package com.abc.xyz;2import com.sun.tools.attach.VirtualMachine;3import com.sun.tools.attach.VirtualMachineDescriptor;4import java.util.List;5import java.io.IOException;6import java.lang.management.ManagementFactory;7import java.lang.management.RuntimeMXBean;8import java.util.ArrayList;9import java.util.List;10import java.util.Map;11import java.util.Set;12import java.util.logging.Level;13import java.util.logging.Logger;14public class 4 {15 public static void main(String[] args) {16 try {17 List<VirtualMachineDescriptor> vmds = VirtualMachine.list();18 for (VirtualMachineDescriptor vmd : vmds) {19 System.out.println("VMID: " + vmd.id() + " VMNAME: " + vmd.displayName());20 }21 RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();22 String jvmName = runtimeBean.getName();23 System.out.println("Current JVM Process ID: " + jvmName);24 int index = jvmName.indexOf('@');25 String pid = jvmName.substring(0, index);26 System.out.println("Current JVM pid: " + pid);27 VirtualMachine vm = VirtualMachine.attach(pid);28 vm.loadAgent("C:\\Users\\Administrator\\Desktop\\agent.jar");29 vm.detach();30 } catch (Exception ex) {31 Logger.getLogger(4.class.getName()).log(Level.SEVERE, null, ex);32 }33 }34}35import java.lang.instrument.Instrumentation;36public class agent {37 public static void premain(String agentArgs, Instrumentation inst) {38 System.out.println("Hello World!");39 }40}

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.*;2public class 4 {3 public static void main(String args[]) throws Exception{4 VirtualMachine vm = VirtualMachine.attach("pid");5 vm.loadAgent("path/to/agent.jar", "any arguments");6 vm.detach();7 }8}

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.*;2import java.io.*;3import java.util.*;4import java.util.jar.*;5import java.util.zip.*;6{7public static void main(String args[])8{9VirtualMachine vm = null;10String pid = "";11{12pid = args[0];13vm = VirtualMachine.attach(pid);14System.out.println("Attached to process " + pid);15String agent = vm.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar";16vm.loadAgent(agent);17System.out.println("Agent loaded");18System.out.println("Getting list of loaded classes");19vm.detach();20}21catch (Exception e)22{23e.printStackTrace();24}25}26}

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1package com.sandbox;2import java.io.IOException;3import java.util.List;4import com.sun.tools.attach.AttachNotSupportedException;5import com.sun.tools.attach.VirtualMachine;6import com.sun.tools.attach.VirtualMachineDescriptor;7public class ListLoadedClassesInRunningJVM {8 public static void main(String[] args) throws AttachNotSupportedException, IOException {9 List<VirtualMachineDescriptor> vms = VirtualMachine.list();10 for (VirtualMachineDescriptor vmd : vms) {11 System.out.println(vmd.displayName());12 }13 VirtualMachine vm = VirtualMachine.attach("12345");14 System.out.println(vm.getSystemProperties());

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1import java.io.IOException;2import java.util.List;3import com.sun.tools.attach.*;4public class 4 {5 public static void main(String[] args) throws IOException {6 List<VirtualMachineDescriptor> vmds = VirtualMachine.list();7 for (VirtualMachineDescriptor vmd : vmds) {8 System.out.println("Process ID: " + vmd.id());9 System.out.println("Display Name: " + vmd.displayName());10 System.out.println("Virtual Machine Identifier: " + vmd11 .id());12 System.out.println("Virtual Machine Provider: " + vmd13 .provider());14 System.out.println("Virtual Machine Name: " + vmd.name());15 System.out.println();16 }17 }18}

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.*;2import java.io.*;3import java.util.*;4public class 4 {5 public static void main(String[] args) throws Exception {6 VirtualMachine.list();7 for (VirtualMachineDescriptor vmd : vmds) {8 System.out.println("JVM Id: " + vmd.id());9 System.out.println("JVM Display Name: " + vmd.displayName());10 System.out.println("JVM Provider: " + vmd.provider());11 System.out.println("JVM Version: " + vmd.version());12 System.out.println("JVM Location: " + vmd.location());13 VirtualMachine vm = VirtualMachine.attach(vmd);14 Properties props = vm.getSystemProperties();15 System.out.println("JVM System Properties: " + props);16 vm.detach();17 }18 }19}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Powermock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful