简体   繁体   中英

How to access set of resource files within a JAR in JAvaagent

I implemented a javaagent and need to access some resource files within the agent's jar file. (eg: log4j.properties file, keystore .jks file etc.) If I put them in the resource folder under src/main they get included in the agent jar.

Agent JAR

  • log4j.properties
  • META-INF
  • agentFolder

But how can I access those files during run time of the agent? How can I give the path to those files within agent's project files. Little explanation would be highly appreciated. If it is not possible I would like to have some suggestions on how to access those configuration file more effectively. Since the agent will be running on different products, I though that giving a folder structure would be inappropriate.

A Java agent is just another Java program. The jar file containing your agent ends up on the class path and can therefore be accessed just like any other resource. For example, try MyAgent.class.getResource("/log4j.properties") to get hold of such a file. This should work as expected in any Java program and work on any platform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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