简体   繁体   English

Java 打包到 jar 文件时代理未运行

[英]Java Agent not running when packaged into jar file

I am trying to implement a java agent to run before an application jar, for PoC i have created 2 Java Projects in Eclipse, one is app.java and agent.java, I am trying to implement a java agent to run before an application jar, for PoC i have created 2 Java Projects in Eclipse, one is app.java and agent.java,

app.java app.java

package test_app;

public class app {

public static void main(String args[]) {
    
    System.out.println("MAIN JAVA CLASS!!!!");
    
}

}

agent.java代理.java

package java_agent;

import java.lang.instrument.*;

public class agent {

public static void premain(String agentArgs, Instrumentation inst) {
    
    System.out.println("INSIDE AGENT");
    
}

}

my manifest file for app.java looks like this,我的 app.java 清单文件看起来像这样,

Main-Class: test_app.app
Class-Path: .

my manifest file for agent.java looks like this,我的agent.java清单文件看起来像这样,

PreMain-Class: java_agent.agent

I have executed for both with these commands,我已经使用这些命令执行了这两个命令,

javac app.java
javac agent.java

jar -cvfm app.jar manifest.txt app.class
jar -cvfm agent.jar manifest.txt agent.class

then I am copying the.jar file sinto another directory and running,然后我将.jar 文件复制到另一个目录并运行,

java -javaagent:agent.jar -jar app.jar

but the combination, it not working and giving errors like this,但是组合,它不起作用并给出这样的错误,

Exception in thread "main" java.lang.ClassNotFoundException: agent_app.agent
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:431)
    at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:525)
 *** java.lang.instrument ASSERTION FAILED ***: "result" with message agent load/premain call failed at ./open/src/java.instrument/share/native/libinstrument/JPLISAgent.c line: 422
 FATAL ERROR in native method: processing of -javaagent failed, processJavaStart failed

Kindly help me to fix this and how to do it.请帮助我解决此问题以及如何解决。 I am doing entirely on Eclipse.我完全在 Eclipse 上做。

It's obviously: you should move your.class file into your specified package(path), and which are test_app and java_agent.很明显:您应该将.class 文件移动到您指定的包(路径)中,它们是test_app 和java_agent。

After you have done this, make the jar and run, it should be ok.完成此操作后,制作 jar 并运行,应该没问题。

Besides, why don't you make the jar using eclipse?此外,为什么不使用 eclipse 制作 jar?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM