Attach Java Agent from All-in-One-Bank

I just wanted to try myself with javassist and started editing the body of the library method. To connect to the library, I attach the agent using the tools.jar tool located in '% JAVA_HOME% .. \ lib \'.

But I do not like the idea of ​​each PC, I use this when installing jdk only for tools.jar

Is there no other way how to extract the input banner in my last jar?
I did it with javassist and it seemed to work fine (IntelliJ did it. It has a nice function for it http://puu.sh/hoiCo/bf19853b12.png )
But with the help of tools.jar, the program that throws an exception

Screenshot with exception http://puu.sh/hoiGd/844567bca2.png

public static void main(String[] args){
    if(args.length < 1){
        log("No ProcessID set");
        return;
    }
    String pid = args[0];
    VirtualMachine vm = null;
    try{
        vm = VirtualMachine.attach(pid);
        String filePath = AgentMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        filePath = URLDecoder.decode(filePath, "UTF-8");
        if(filePath.startsWith("/")){
            filePath = filePath.substring(1);
        }
        log("Loading Agent... [" + filePath + "]");
        vm.loadAgent(filePath);
    }catch(Exception ex){
        log("VM connection error [" + pid + "]");
        ex.printStackTrace();
    }finally{
        try{
            if(vm != null) vm.detach();
        }catch(Exception ex){}
    }
}

, .

, - .
, :)

+4
1

: /-

public class HelloAgent
{
    public static void agentmain(String agentArgs, Instrumentation inst)
    {
        System.out.println(agentArgs);
        System.out.println("Hi from the agent!");
        System.out.println("I've got instrumentation!: " + inst);
    }
}

public static void main(String[] args)
{
    AgentLoader.loadAgentClass(HelloAgent.class.getName(), "Hello!");
}

VirtualMachine , jar .

maven:

<dependency>
    <groupId>com.ea.orbit</groupId>
    <artifactId>orbit-agent-loader</artifactId>
    <version>0.5.2</version>
</dependency>

.


, , VM: -javaagent:your-agent.jar

: Intellij java- , -javaagent: something.jar. , , . ( , - /, ).

+1

Source: https://habr.com/ru/post/1584348/


All Articles