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

Best Powermock code snippet using com.sun.tools.attach.VirtualMachine.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.VirtualMachine;2import com.sun.tools.attach.VirtualMachineDescriptor;3import java.util.List;4public class 4 {5 public static void main(String[] args) {6 List<VirtualMachineDescriptor> vms = VirtualMachine.list();7 for (VirtualMachineDescriptor vm : vms) {8 try {9 VirtualMachine v = VirtualMachine.attach(vm);10 System.out.println(v.getSystemProperties());11 System.out.println(v.getAgentProperties());12 System.out.println(v.getSystemProperties());13 System.out.println(v.getAgentProperties());14 v.detach();15 } catch (Exception e) {16 e.printStackTrace();17 }18 }19 }20}

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1import java.io.*;2import java.util.*;3import com.sun.tools.attach.*;4public class 4 {5 public static void main(String args[]) throws Exception {6 String pid = args[0];7 VirtualMachine vm = VirtualMachine.attach(pid);8 Properties props = vm.getSystemProperties();9 vm.detach();10 props.list(System.out);11 }12}

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.*;2import java.util.*;3{4public static void main(String args[])5{6List l=VirtualMachine.list();7System.out.println("Total Java Processes Running: "+l.size());8for(int i=0;i<l.size();i++)9{10VirtualMachineDescriptor vmd=(VirtualMachineDescriptor)l.get(i);11System.out.println("Process ID: "+vmd.id());12System.out.println("Process Display Name: "+vmd.displayName());13}14}15}

Full Screen

Full Screen

VirtualMachine

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.*;2import java.io.IOException;3import java.util.Properties;4import java.io.File;5import java.io.FileOutputStream;6import java.io.InputStream;7import java.io.OutputStream;8import java.io.BufferedInputStream;9import java.io.BufferedOutputStream;10import java.io.BufferedReader;11import java.io.InputStreamReader;12import java.io.FileInputStream;13import java.io.FileNotFoundException;14import java.io.IOException;15import java.util.ArrayList;16import java.util.List;17import java.util.logging.Level;18import java.util.logging.Logger;19public class 4 {20 public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {21 String pid = args[0];22 String heapDumpPath = args[1];23 String agent = args[2];24 VirtualMachine vm = VirtualMachine.attach(pid);25 vm.loadAgent(agent, heapDumpPath);26 vm.detach();27 }28}29import com.sun.tools.attach.*;30import java.io.IOException;31import java.util.Properties;32import java.io.File;33import java.io.FileOutputStream;34import java.io.InputStream;35import java.io.OutputStream;36import java.io.BufferedInputStream;37import java.io.BufferedOutputStream;38import java.io.BufferedReader;39import java.io.InputStreamReader;40import java.io.FileInputStream;41import java.io.FileNotFoundException;42import java.io.IOException;43import java.util.ArrayList;44import java.util.List;45import java.util.logging.Level;46import java.util.logging.Logger;47public class 5 {48 public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {49 String pid = args[0];50 String heapDumpPath = args[1];51 String agent = args[2];52 VirtualMachine vm = VirtualMachine.attach(pid);53 vm.loadAgent(agent, heapDumpPath);54 vm.detach();55 }56}57import com.sun.tools.attach.*;58import java.io.IOException;59import java.util.Properties;60import java.io.File;61import java.io.FileOutputStream;62import java.io.InputStream;63import java.io.OutputStream;64import java.io.BufferedInputStream;65import java.io.BufferedOutputStream;66import java.io.BufferedReader;67import java.io.InputStreamReader;68import java.io.FileInputStream;69import java.io.FileNotFoundException;70import java.io.IOException;71import java.util

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful