java agent

package com.test;

import java.io.*;
import java.lang.instrument.*;
import java.security.*;

public class dumper
{
//A java agent must have a premain method which acts as the entry-point
public static void premain(String agentArgs, Instrumentation inst)
{
System.out.println("agent loaded");

// Register our transformer
inst.addTransformer(new transformer());
}
}

class transformer implements ClassFileTransformer
{
String destFolder = "d:/test/";
// The transform method is called for each non-system class as they are being loaded
public byte[] transform(ClassLoader loader, String className,
Class<?> classBeingRedefined, ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws IllegalClassFormatException
{
if (className != null)
{
// Skip all system classes
// if (!className.startsWith("java") &&
// !className.startsWith("sun") &&
// !className.startsWith("javax") &&
// !className.startsWith ("com") &&
// !className.startsWith("jdk") &&
// !className.startsWith("org"))
// {
if(className.startsWith ("com/fr"))
{
System.out.println("Dumping: " + className);

// Replace all separator charactors
//String newName = className.replaceAll("/", "#") + ".class";
String newName = destFolder + className.replace(".", "/") + ".class";

File destFile = new File(newName);
File destDirectory = destFile.getParentFile();
destDirectory.mkdirs();

try
{
FileOutputStream fos = new FileOutputStream(newName);
fos.write(classfileBuffer);
fos.close();
}
catch (Exception ex)
{
System.out.println("Exception while writing: " + newName);
}
}
}
// We are not modifying the bytecode in anyway, so return it as-is
return classfileBuffer;
}
}






MANIFEST.MF

Manifest-Version: 1.0
Premain-Class: com.test.dumper

@echo 此Bat将启动大内存版本的设计器
..\jre\bin\java -javaagent:jagent.jar -Xms512m -Xmx1024m -cp ../lib/*;../lib/jetty/*;../WebReport/WEB-INF/lib/* com.fr.start.Designer demo

posted @ 2018-03-18 16:43  飞晨信息  阅读(176)  评论(0)    收藏  举报