Skip to content

compatible different command line style for find jar path #12544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 6, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ Path detectJarPath() {
@Nullable
private Path getJarPathFromProcessHandle() {
String[] javaArgs = getProcessHandleArguments.get();
for (int i = 0; i < javaArgs.length; ++i) {
if ("-jar".equals(javaArgs[i]) && (i < javaArgs.length - 1)) {
return Paths.get(javaArgs[i + 1]);
boolean jarOptionFound = false;
for (String javaArg : javaArgs) {
if ("-jar".equals(javaArg)) {
jarOptionFound = true;
} else if (jarOptionFound
&& !javaArg.startsWith(
"-")) { // flags can appear between -jar and the jar path, ignore them
return Paths.get(javaArg);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ void createResource_processHandleJar() {
.containsEntry(ServiceAttributes.SERVICE_NAME, "my-service");
}

@Test
void createResource_processHandleJarExtraFlag() {
String path = Paths.get("path", "to", "app", "my-service.jar").toString();
JarServiceNameDetector serviceNameProvider =
getDetector(
new String[] {"-Dtest=42", "-jar", "-Xmx512m", path, "abc", "def"},
prop -> null,
JarServiceNameDetectorTest::failPath);

Resource resource = serviceNameProvider.createResource(config);

assertThat(resource.getAttributes())
.hasSize(1)
.containsEntry(ServiceAttributes.SERVICE_NAME, "my-service");
}

@Test
void createResource_processHandleJarWithoutExtension() {
JarServiceNameDetector serviceNameProvider =
Expand Down
Loading