{"record":{"id":"f19d3cafd750bdef","repo":"microsoft/aspire","slug":"java-application-resource-name-cannot-be-published-because","errorCode":null,"errorMessage":"Java application '{resource.Name}' cannot be published because its jarPath '{annotation.JarPath}' is outside the directory the build runs in. The path is resolved against the application directory inside the container, so it has to name a file the build produces there. Pass a jarPath relative to the application directory, or use WithJarArtifact to name the published artifact separately from the one run locally.","messagePattern":"Java application '(.+?)' cannot be published because its jarPath '(.+?)' is outside the directory the build runs in\\. The path is resolved against the application directory inside the container, so it has to name a file the build produces there\\. Pass a jarPath relative to the application directory, or use WithJarArtifact to name the published artifact separately from the one run locally\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Java/JavaDockerfileGenerator.cs","lineNumber":355,"sourceCode":"            return false;\n        }\n\n        // Container paths are POSIX even when the AppHost authored a Windows-style relative path.\n        var normalized = annotation.JarPath.Replace('\\\\', '/');\n\n        if (normalized.StartsWith(\"./\", StringComparison.Ordinal))\n        {\n            normalized = normalized[2..];\n        }\n\n        if (normalized.Length == 0)\n        {\n            return false;\n        }\n\n        if (IsPathRootedOnAnyPlatform(annotation.JarPath) || normalized.Split('/').Contains(\"..\"))\n        {\n            throw new DistributedApplicationException(\n                $\"Java application '{resource.Name}' cannot be published because its jarPath \" +\n                $\"'{annotation.JarPath}' is outside the directory the build runs in. The path is resolved \" +\n                \"against the application directory inside the container, so it has to name a file the \" +\n                \"build produces there. Pass a jarPath relative to the application directory, or use \" +\n                \"WithJarArtifact to name the published artifact separately from the one run locally.\");\n        }\n\n        jarPath = normalized;\n\n        return true;\n    }\n\n    /// <summary>\n    /// Normalizes an authored path for use inside the container build, rejecting anything that would\n    /// reach outside the build context.\n    /// </summary>\n    /// <remarks>\n    /// The build context is the application directory, so only files under it are uploaded to the daemon.","sourceCodeStart":337,"sourceCodeEnd":373,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Java/JavaDockerfileGenerator.cs#L337-L373","documentation":"During Dockerfile generation for publish mode, TryGetBuildOutputJarPath validates the JavaBuildToolAnnotation.JarPath. A jarPath that is rooted (e.g. C:\\... or /...) or contains a '..' segment resolves outside the application directory inside the container build, so generation throws DistributedApplicationException rather than producing a broken COPY. The jar must be a relative path within the build context.","triggerScenarios":"Calling WithJarBuild (or setting JarPath on JavaBuildToolAnnotation) with an absolute path like \"/app/target/app.jar\" or \"C:\\build\\app.jar\", or a relative path like \"../out/app.jar\", then running the publish/Dockerfile generation.","commonSituations":"Copying a local machine's absolute jar path into the resource configuration; assuming paths resolve on the host instead of inside the build container; using '..' to point at a sibling module's output directory.","solutions":["Change jarPath to a path relative to the application directory, e.g. \"target/app.jar\" (Maven) or \"build/libs/app.jar\" (Gradle).","If you need the locally-run jar and the published jar to differ, use WithJarArtifact to name the published artifact separately.","Remove any leading slashes, drive letters, or '..' segments from jarPath."],"exampleFix":"// before\njava.WithJarBuild(JavaBuildTool.Maven, jarPath: \"/home/dev/target/app-1.0.jar\");\n// after\njava.WithJarBuild(JavaBuildTool.Maven, jarPath: \"target/app-1.0.jar\");","handlingStrategy":"validation","validationCode":"var normalized = jarPath.Replace('\\\\', '/');\nif (Path.IsPathRooted(jarPath) || normalized.Split('/').Contains(\"..\"))\n    throw new ArgumentException(\"jarPath must be relative to the application directory, e.g. target/app.jar\");","typeGuard":null,"tryCatchPattern":"try { await PublishAsync(...); } catch (DistributedApplicationException ex) when (ex.Message.Contains(\"jarPath\")) { Console.Error.WriteLine(ex.Message); return 1; }","preventionTips":["Always author jarPath relative to the application directory (e.g. target/app.jar, build/libs/app.jar).","Never copy absolute host paths into resource configuration.","Use WithJarArtifact when the local and published artifacts differ."],"tags":["java","docker","publish","path-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}