本文整理汇总了Java中com.sun.tools.attach.AgentInitializationException类的典型用法代码示例。如果您正苦于以下问题:Java AgentInitializationException类的具体用法?Java AgentInitializationException怎么用?Java AgentInitializationException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AgentInitializationException类属于com.sun.tools.attach包,在下文中一共展示了AgentInitializationException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.sun.tools.attach.AgentInitializationException; //导入依赖的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();
}
示例2: main
import com.sun.tools.attach.AgentInitializationException; //导入依赖的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);
}
}
示例3: loadAgentLibrary
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
throws AgentLoadException, AgentInitializationException, IOException
{
InputStream in = execute("load",
agentLibrary,
isAbsolute ? "true" : "false",
options);
try {
int result = readInt(in);
if (result != 0) {
throw new AgentInitializationException("Agent_OnAttach failed", result);
}
} finally {
in.close();
}
}
示例4: attach
import com.sun.tools.attach.AgentInitializationException; //导入依赖的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();
}
}
示例5: testBadModification2
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
@Test
public void testBadModification2() throws ClassNotFoundException, AgentLoadException, AgentInitializationException, IOException, AttachNotSupportedException, UnmodifiableClassException, IllegalConnectorArgumentsException {
// Rewrite method
DynamicModification badModification = new DynamicModification() {
public Collection<String> affects() {
return Lists.newArrayList(TestJDWPAgentDebug.class.getName());
}
public void apply(ClassPool arg0) throws NotFoundException, CannotCompileException {
CtClass cls = arg0.getCtClass(TestJDWPAgentDebug.class.getName());
cls.getMethods()[0].insertBefore("definitely not code...");
}
};
JDWPAgent dynamic = JDWPAgent.get();
// Modification should just be ignored since it throws a notfoundexception
try {
dynamic.install(badModification);
fail();
} catch (CannotCompileException e) {
}
}
示例6: testBadModification2
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
@Test
public void testBadModification2() throws ClassNotFoundException, AgentLoadException, AgentInitializationException, IOException, AttachNotSupportedException, UnmodifiableClassException {
// Rewrite method
DynamicModification badModification = new DynamicModification() {
public Collection<String> affects() {
return Lists.newArrayList(TestJVMAgent.class.getName());
}
public void apply(ClassPool arg0) throws NotFoundException, CannotCompileException {
CtClass cls = arg0.getCtClass(TestJVMAgent.class.getName());
cls.getMethods()[0].insertBefore("definitely not code...");
}
};
JVMAgent dynamic = JVMAgent.get();
// Modification should just be ignored since it throws a notfoundexception
try {
dynamic.install(badModification);
fail();
} catch (CannotCompileException e) {
}
}
示例7: loadAgentLibrary
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
/**
* Load agent library.
* If isAbsolute is true then the agent library is the absolute path to the library and thus
* will not be expanded in the target VM.
* If isAbsolute is false then the agent library is just a library name and it will be expended
* in the target VM.
*/
private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
throws AgentLoadException, AgentInitializationException, IOException
{
InputStream in = execute("load", agentLibrary, isAbsolute ? "true" : "false", options);
try {
int result = readInt(in);
if (result != 0) {
throw new AgentInitializationException("Agent_OnAttach failed", result);
}
}
finally {
in.close();
}
}
示例8: init
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
private void init(File agentJar) throws AgentInitializationException, AgentLoadException, AttachNotSupportedException, IOException {
reloadIntervals = 5;
classFileInfos = new ArrayList<ClassFileInfo>();
excludePackages = TObject.asList("java.","sun.","javax.","com.sun","com.oracle");
if(agentJar == null) {
agentJar = findAgentJar();
}
if(agentJar != null && agentJar.exists()) {
Logger.info("[HOTSWAP] System choose an agent jar file: "+agentJar.getAbsolutePath());
agentAttach(agentJar.getPath());
loadCustomClass();
} else {
throw new FileNotFoundException("The agent jar file not found");
}
}
示例9: connectorAddress
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
/**
* Get a connection address for a local management agent. If no such agent exists, one will be
* started.
*
* @param vm
* instance representing a running JVM
* @return the management agent connection string
* @throws IOException
* if an I/O error occurs while communicating with the VM
*/
private String connectorAddress(VirtualMachine vm) throws IOException {
String address = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
if (address == null) {
// No locally connectable agent, install one.
try {
String javaHome = vm.getSystemProperties().getProperty("java.home");
vm.loadAgent(Joiner.on(File.separator).join(javaHome, "lib", "management-agent.jar"));
}
catch (AgentLoadException | AgentInitializationException e) {
throw Throwables.propagate(e);
}
address = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
}
return address;
}
示例10: loadJMXAgent
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
public static String loadJMXAgent(int port) throws IOException, AttachNotSupportedException, AgentLoadException,
AgentInitializationException {
System.setProperty("com.sun.management.jmxremote.port", Integer.toString(port));
String name = ManagementFactory.getRuntimeMXBean().getName();
VirtualMachine vm = VirtualMachine.attach(name.substring(0, name.indexOf('@')));
String lca = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
if (lca == null) {
Path p = Paths.get(System.getProperty("java.home")).normalize();
if (!"jre".equals(p.getName(p.getNameCount() - 1).toString().toLowerCase()))
p = p.resolve("jre");
File f = p.resolve("lib").resolve("management-agent.jar").toFile();
if (!f.exists())
throw new IOException("Management agent not found");
vm.loadAgent(f.getCanonicalPath(), "com.sun.management.jmxremote");
lca = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
}
vm.detach();
return lca;
}
示例11: main
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
public static void main(String[] args) throws AgentLoadException, AgentInitializationException, IOException, AttachNotSupportedException {
VirtualMachine vm=VirtualMachine.attach("7876");
vm.loadAgent("d:/springloaded-1.2.1.RELEASE.jar");
// vm.loadAgent("d:/myagent.jar","myagent");
System.out.println(vm.getAgentProperties().toString());
System.in.read();
}
示例12: main
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
public static void main(String[] args) {
try {
AgentLoader.attach(TestAgent.class);
} catch (AgentInstantiationException | AgentLoadException | AgentInitializationException | AttachNotSupportedException | NotFoundException | CannotCompileException | IOException e) {
e.printStackTrace();
}
}
示例13: loadAgent
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
public void loadAgent(String agent, String options)
throws AgentLoadException, AgentInitializationException, IOException
{
String args = agent;
if (options != null) {
args = args + "=" + options;
}
try {
loadAgentLibrary("instrument", args);
} catch (AgentInitializationException x) {
/*
* Translate interesting errors into the right exception and
* message (FIXME: create a better interface to the instrument
* implementation so this isn't necessary)
*/
int rc = x.returnValue();
switch (rc) {
case JNI_ENOMEM:
throw new AgentLoadException("Insuffient memory");
case ATTACH_ERROR_BADJAR:
throw new AgentLoadException(
"Agent JAR not found or no Agent-Class attribute");
case ATTACH_ERROR_NOTONCP:
throw new AgentLoadException(
"Unable to add JAR file to system class path");
case ATTACH_ERROR_STARTFAIL:
throw new AgentInitializationException(
"Agent JAR loaded but agent failed to initialize");
default :
throw new AgentLoadException("" +
"Failed to load agent - unknown reason: " + rc);
}
}
}
示例14: attachJmx
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
public static String attachJmx(String pid) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
VirtualMachine vm = null;
try {
vm = VirtualMachine.attach(pid);
String serviceUrl = ConnectorAddressLink.importFrom(Integer.parseInt(vm.id().trim()));
if(serviceUrl == null) {
String home = System.getProperty("java.home");
String agent = home + File.separator + "jre" + File.separator + "lib"
+ File.separator + "management-agent.jar";
File f = new File(agent);
if (!f.exists()) {
agent = home + File.separator + "lib" + File.separator +
"management-agent.jar";
f = new File(agent);
if (!f.exists()) {
throw new RuntimeException("management-agent.jar missing");
}
}
agent = f.getCanonicalPath();
__.println("Loading " + agent + " into target VM ...");
vm.loadAgent(agent);
serviceUrl = ConnectorAddressLink.importFrom(Integer.parseInt(vm.id().trim()));
}
return serviceUrl;
} finally {
if(vm != null) {
try {
vm.detach();
} catch (IOException e) {}
}
}
}
示例15: get
import com.sun.tools.attach.AgentInitializationException; //导入依赖的package包/类
/** Try to start an agent. Wait for it to attach, or throw an exception */
public static JVMAgent get() throws AgentLoadException, AgentInitializationException, IOException, AttachNotSupportedException {
// There may be only one
if (instance != null) {
return instance;
}
// Not guaranteed to be correct, but seems to work.
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
VirtualMachine jvm = VirtualMachine.attach(pid);
// Gets the current path
String path = JVMAgent.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
jvm.loadAgent(decodedPath, null);
// Wait for the instance, time out relatively quickly
try {
if (!waitForInstance.await(2000, TimeUnit.MILLISECONDS)) {
System.err.println("Unable to create JVM agent: timed out waiting for JVM agent to attach");
return null;
}
} catch (InterruptedException e) {
System.err.println("Unable to create JVM agent: interrupted waiting for JVM agent to attach");
return null;
}
if (instance == null) {
System.err.println("Unable to create JVM agent");
} else {
System.out.println("Successfully attached JVM agent");
}
// Return the instance, which might be null
synchronized(JVMAgent.class) {
return instance;
}
}