Hellooooo, I am new to Stack Overflow,I'm clumsy when it comes to asking questions. But I will provide additional information if the question is unclear. This is the issue I encountered, as follows:
Background: I have two projects, one is my business project A, and the other is my Java Agent project B. I launch them using 'java -javaagent:B.jar -jar A.jar' method.
Divergence: Currently, there are two different approaches:
(I) I place B.jar and related configuration files in the "serial" directory under the parent directory Base_Dir of A.jar and start them using a Shell script.
(II) My supervisor wants me to package the "serial" directory into A.jar.
Questions: Regarding the above divergence, I have two questions:
(I) Is it feasible to package the "serial" directory into A.jar in a Java project?
(II) Typically, what are the more common practices or best practices for addressing this type of project structure issue?
If I receive any response from you, I will be greatly appreciative.
Directory Structure:
I can't post images in my question, this is my structure:
C:.
│ SerialNumberAppDemo-1.0-SNAPSHOT.jar
│
└─serial
ConfoundLogback.xml
demo-1.jar
readme.md
sample-configs.properties
business jar and serial directory the content of serial directory
When B.jar
is contained in A.jar
, the option -javaagent:B.jar
does not work, as this option looks for a file in the default filesystem, not inside another jar file.
Since Java 9, you can specify the Launcher-Agent-Class
attribute in the manifest of A.jar
to specify an Agent class within the same jar file, to be launched automatically when starting the application with -jar A.jar
(so -javaagent
is not needed then). This requires the classes of the Java Agent (of B.jar
) to be added directly to A.jar
instead of adding B.jar
to A.jar
as a single entry.
When adding the other resources, you might have to adapt the Agent’s code to use getResource
or getResourceAsStream
instead of looking of a file beside the jar file in the default filesystem in case it needs to read one of them (e.g. ConfoundLogback.xml
).
Other files, e.g. readme.md
and sample-configs.properties
, do not look like being needed by the Agent but intended to be read by other tools or even humans. Whether it is sufficient for them to have these files inside the jar file instead of immediately visible in the directory, is a question beyond the scope of Stackoverflow.
To sum it up, just adding the serial
directory to the application’s jar file will not work.