{"record":{"id":"6f10cd51ecbfc05c","repo":"microsoft/aspire","slug":"methodname-cannot-be-used-when-a-jar-path-has-been-specified","errorCode":null,"errorMessage":"{methodName} cannot be used when a JAR path has been specified. Use either the AddJavaApp overload that takes a jarPath, or {methodName}, not both.","messagePattern":"(.+?) cannot be used when a JAR path has been specified\\. Use either the AddJavaApp overload that takes a jarPath, or (.+?), not both\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Java/JavaHostingExtensions.cs","lineNumber":693,"sourceCode":"        ArgumentException.ThrowIfNullOrWhiteSpace(task);\n        ArgumentNullException.ThrowIfNull(args);\n\n        return builder.WithBuildToolLaunch(JavaBuildTool.Gradle, task, args, nameof(WithGradleTask));\n    }\n\n    private static IResourceBuilder<T> WithBuildToolLaunch<T>(\n        this IResourceBuilder<T> builder,\n        JavaBuildTool tool,\n        string goalOrTask,\n        string[] args,\n        string methodName) where T : JavaAppResource\n    {\n        // A prebuilt JAR and a build-tool launch are mutually exclusive: the build tool decides what to\n        // run, so -jar would be ignored. CommunityToolkit rejects both combinations; this port only\n        // rejected the Gradle half, letting a Maven+JAR application silently drop its JAR.\n        if (builder.Resource.HasAnnotationOfType<JavaJarPathAnnotation>())\n        {\n            throw new InvalidOperationException(\n                $\"{methodName} cannot be used when a JAR path has been specified. Use either the \" +\n                $\"{nameof(AddJavaApp)} overload that takes a jarPath, or {methodName}, not both.\");\n        }\n\n        if (builder.Resource.TryGetLastAnnotation<JavaBuildToolAnnotation>(out var existing) && existing.Tool != tool)\n        {\n            throw new InvalidOperationException(\n                $\"{methodName} cannot be used when the application is already configured to launch with \" +\n                $\"{existing.Tool}. A Java application is launched by a single build tool.\");\n        }\n\n        if (builder.Resource.TryGetLastAnnotation<JavaBuildStepAnnotation>(out var buildStep) && buildStep.Tool != tool)\n        {\n            throw new InvalidOperationException(\n                $\"{methodName} cannot be used when the application is already configured to build with \" +\n                $\"{buildStep.Tool}. A Java application is built and launched by a single build tool.\");\n        }\n","sourceCodeStart":675,"sourceCodeEnd":711,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Java/JavaHostingExtensions.cs#L675-L711","documentation":"Aspire's Java hosting support allows launching a Java application either from a prebuilt JAR (AddJavaApp with jarPath) or through a build tool like Maven/Gradle. These two launch modes are mutually exclusive because the build tool decides what to run, so any -jar argument would be silently ignored. The library throws this InvalidOperationException when WithBuildToolLaunch is called on a resource that already has a JavaJarPathAnnotation.","triggerScenarios":"Calling WithMavenGoal or WithGradleTask (which route through WithBuildToolLaunch) on a resource created via the AddJavaApp overload that takes a jarPath.","commonSituations":"Switching a resource from a prebuilt-JAR setup to a build-tool run by adding a Maven/Gradle launch call without removing the jarPath overload; copy-pasting resource configuration between projects that use different launch styles.","solutions":["Remove the jarPath argument from the AddJavaApp call and use the overload without jarPath","Remove the WithMavenGoal/WithGradleTask call if you intend to run the prebuilt JAR","Choose one launch mode: JAR path or build tool, never both"],"exampleFix":"// before\nvar java = builder.AddJavaApp(\"app\", workingDirectory: \"..\").WithJarPath(\"app.jar\").WithMavenGoal(\"spring-boot:run\");\n// after\nvar java = builder.AddJavaApp(\"app\", workingDirectory: \"..\").WithMavenGoal(\"spring-boot:run\");","handlingStrategy":"validation","validationCode":"if (resource.HasAnnotationOfType<JavaJarPathAnnotation>())\n{\n    throw new InvalidOperationException(\"Do not call WithMavenGoal/WithGradleTask on a JAR-based Java app resource.\");\n}","typeGuard":"var isJarBased = builder.Resource.HasAnnotationOfType<JavaJarPathAnnotation>();","tryCatchPattern":"try { java.WithMavenGoal(\"spring-boot:run\"); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"JAR path has been specified\"))\n{\n    // pick one launch mode: drop the jarPath or drop the build-tool launch\n}","preventionTips":["Use the AddJavaApp overload with jarPath only for prebuilt JARs","Never combine JAR-path and build-tool launch APIs on the same resource","Centralize Java resource creation in one helper that picks a single launch mode"],"tags":["java","aspire-hosting","invalid-configuration","build-tool"],"backgroundTag":"mutually-exclusive-options","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"}