org.gradle.initialization.exception.InitializationException: Gradle could not start your build

Getting a gradle build error

D:\Java\Spring\demo2\demo>gradle clean build --stacktrace

Welcome to Gradle 8.0.2!

Here are the highlights of this release:
 - Improvements to the Kotlin DSL
 - Fine-grained parallelism from the first build with configuration cache
 - Configurable Gradle user home cache cleanup

For more details see https://docs.gradle.org/8.0.2/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)

FAILURE: Build failed with an exception.

* What went wrong:
Gradle could not start your build.
> Could not create service of type BuildLifecycleController using ServicesProvider.createBuildLifecycleController().
   > Could not create service of type GradleInternal using ServicesProvider.createGradleModel().
      > Could not create an instance of type org.gradle.invocation.DefaultGradle.
         > Could not configure services using GradleScopeCompileServices.configure().

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.initialization.exception.InitializationException: Gradle could not start your build.
        at org.gradle.initialization.exception.DefaultExceptionAnalyser.collectFailures(DefaultExceptionAnalyser.java:90)
        at org.gradle.initialization.exception.MultipleBuildFailuresExceptionAnalyser.transform(MultipleBuildFailuresExceptionAnalyser.java:55)
        at org.gradle.initialization.exception.MultipleBuildFailuresExceptionAnalyser.transform(MultipleBuildFailuresExceptionAnalyser.java:38)
        at org.gradle.initialization.exception.StackTraceSanitizingExceptionAnalyser.transform(StackTraceSanitizingExceptionAnalyser.java:33)
        at org.gradle.tooling.internal.provider.SessionFailureReportingActionExecuter.execute(SessionFailureReportingActionExecuter.java:59)
        at org.gradle.tooling.internal.provider.SessionFailureReportingActionExecuter.execute(SessionFailureReportingActionExecuter.java:38)
        at org.gradle.tooling.internal.provider.SetupLoggingActionExecuter.execute(SetupLoggingActionExecuter.java:47)
        at org.gradle.tooling.internal.provider.SetupLoggingActionExecuter.execute(SetupLoggingActionExecuter.java:31)
        at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:65)
        at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:37)
        at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
        at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:39)
        at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
        at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:29)
        at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
        at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:35)
        at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
        at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.create(ForwardClientInput.java:78)
        at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.create(ForwardClientInput.java:75)
        at org.gradle.util.internal.Swapper.swap(Swapper.java:38)
        at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:75)
        at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
        at org.gradle.launcher.daemon.server.exec.LogAndCheckHealth.execute(LogAndCheckHealth.java:64)
        at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
        at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:63)
        at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:37)
        at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
        at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:84)
        at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:37)
        at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
        at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:52)        at org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:297)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:49)
Caused by: org.gradle.internal.service.ServiceCreationException: Could not create service of type BuildLifecycleController using ServicesProvider.createBuildLifecycleController().
        at org.gradle.internal.service.DefaultServiceRegistry$FactoryMethodService.invokeMethod(DefaultServiceRegistry.java:901)
        at org.gradle.internal.service.DefaultServiceRegistry$FactoryService.createServiceInstance(DefaultServiceRegistry.java:826)
        at org.gradle.internal.service.DefaultServiceRegistry$ManagedObjectServiceProvider.getInstance(DefaultServiceRegistry.java:611)
        at org.gradle.internal.service.DefaultServiceRegistry$SingletonService.get(DefaultServiceRegistry.java:674)
        at org.gradle.internal.service.DefaultServiceRegistry.find(DefaultServiceRegistry.java:318)
        at org.gradle.internal.service.DefaultServiceRegistry.get(DefaultServiceRegistry.java:302)
        at org.gradle.internal.service.DefaultServiceRegistry.get(DefaultServiceRegistry.java:297)
        at org.gradle.internal.build.AbstractBuildState.lambda$new$0(AbstractBuildState.java:46)
        at org.gradle.internal.lazy.LockingLazy.get(LockingLazy.java:60)
        at org.gradle.internal.build.AbstractBuildState.getBuildController(AbstractBuildState.java:104)
        at org.gradle.composite.internal.DefaultRootBuildState.<init>(DefaultRootBuildState.java:66)
        at org.gradle.composite.internal.BuildStateFactory.createRootBuild(BuildStateFactory.java:66)
        at org.gradle.composite.internal.DefaultIncludedBuildRegistry.createRootBuild(DefaultIncludedBuildRegistry.java:87)
        at org.gradle.launcher.exec.RootBuildLifecycleBuildActionExecutor.execute(RootBuildLifecycleBuildActionExecutor.java:39)
        at org.gradle.internal.buildtree.DefaultBuildTreeContext.execute(DefaultBuildTreeContext.java:40)
        at org.gradle.launcher.exec.BuildTreeLifecycleBuildActionExecutor.lambda$execute$0(BuildTreeLifecycleBuildActionExecutor.java:65)
        at org.gradle.internal.buildtree.BuildTreeState.run(BuildTreeState.java:53)
        at org.gradle.launcher.exec.BuildTreeLifecycleBuildActionExecutor.execute(BuildTreeLifecycleBuildActionExecutor.java:65)
        at org.gradle.launcher.exec.RunAsBuildOperationBuildActionExecutor$3.call(RunAsBuildOperationBuildActionExecutor.java:61)
        at org.gradle.launcher.exec.RunAsBuildOperationBuildActionExecutor$3.call(RunAsBuildOperationBuildActionExecutor.java:57)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:199)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:73)
        at org.gradle.launcher.exec.RunAsBuildOperationBuildActionExecutor.execute(RunAsBuildOperationBuildActionExecutor.java:57)
        at org.gradle.launcher.exec.RunAsWorkerThreadBuildActionExecutor.lambda$execute$0(RunAsWorkerThreadBuildActionExecutor.java:36)
        at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:249)
        at org.gradle.internal.work.DefaultWorkerLeaseService.runAsWorkerThread(DefaultWorkerLeaseService.java:109)
        at org.gradle.launcher.exec.RunAsWorkerThreadBuildActionExecutor.execute(RunAsWorkerThreadBuildActionExecutor.java:36)
        at org.gradle.tooling.internal.provider.continuous.ContinuousBuildActionExecutor.execute(ContinuousBuildActionExecutor.java:110)
        at org.gradle.tooling.internal.provider.SubscribableBuildActionExecutor.execute(SubscribableBuildActionExecutor.java:64)
        at org.gradle.internal.session.DefaultBuildSessionContext.execute(DefaultBuildSessionContext.java:46)
        at org.gradle.tooling.internal.provider.BuildSessionLifecycleBuildActionExecuter$ActionImpl.apply(BuildSessionLifecycleBuildActionExecuter.java:100)
        at org.gradle.tooling.internal.provider.BuildSessionLifecycleBuildActionExecuter$ActionImpl.apply(BuildSessionLifecycleBuildActionExecuter.java:88)
        at org.gradle.internal.session.BuildSessionState.run(BuildSessionState.java:69)
        at org.gradle.tooling.internal.provider.BuildSessionLifecycleBuildActionExecuter.execute(BuildSessionLifecycleBuildActionExecuter.java:62)
        at org.gradle.tooling.internal.provider.BuildSessionLifecycleBuildActionExecuter.execute(BuildSessionLifecycleBuildActionExecuter.java:41)
        at org.gradle.tooling.internal.provider.StartParamsValidatingActionExecuter.execute(StartParamsValidatingActionExecuter.java:63)
        at org.gradle.tooling.internal.provider.StartParamsValidatingActionExecuter.execute(StartParamsValidatingActionExecuter.java:31)
        at org.gradle.tooling.internal.provider.SessionFailureReportingActionExecuter.execute(SessionFailureReportingActionExecuter.java:50)
        ... 29 more
Caused by: org.gradle.internal.service.ServiceCreationException: Could not create service of type GradleInternal using ServicesProvider.createGradleModel().
        at org.gradle.internal.service.DefaultServiceRegistry$FactoryMethodService.invokeMethod(DefaultServiceRegistry.java:901)
        at org.gradle.internal.service.DefaultServiceRegistry$FactoryService.createServiceInstance(DefaultServiceRegistry.java:826)
        at org.gradle.internal.service.DefaultServiceRegistry$ManagedObjectServiceProvider.getInstance(DefaultServiceRegistry.java:611)
        at org.gradle.internal.service.DefaultServiceRegistry$SingletonService.get(DefaultServiceRegistry.java:674)
        at org.gradle.internal.service.DefaultServiceRegistry.find(DefaultServiceRegistry.java:318)
        at org.gradle.internal.service.DefaultServiceRegistry.get(DefaultServiceRegistry.java:302)
        at org.gradle.internal.service.DefaultServiceRegistry.get(DefaultServiceRegistry.java:297)
        at org.gradle.internal.build.DefaultBuildLifecycleControllerFactory.newInstance(DefaultBuildLifecycleControllerFactory.java:88)
        at org.gradle.configurationcache.DefaultBuildModelControllerServices$ServicesProvider.createBuildLifecycleController(DefaultBuildModelControllerServices.kt:116)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:125)
        at org.gradle.internal.service.ReflectionBasedServiceMethod.invoke(ReflectionBasedServiceMethod.java:34)
        at org.gradle.internal.service.DefaultServiceRegistry$FactoryMethodService.invokeMethod(DefaultServiceRegistry.java:899)
        ... 72 more
Caused by: org.gradle.api.reflect.ObjectInstantiationException: Could not create an instance of type org.gradle.invocation.DefaultGradle.
        at org.gradle.internal.instantiation.generator.DependencyInjectingInstantiator.doCreate(DependencyInjectingInstantiator.java:69)
        at org.gradle.internal.instantiation.generator.DependencyInjectingInstantiator.newInstance(DependencyInjectingInstantiator.java:55)
        at org.gradle.configurationcache.DefaultBuildModelControllerServices$ServicesProvider.createGradleModel(DefaultBuildModelControllerServices.kt:107)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:125)
        at org.gradle.internal.service.ReflectionBasedServiceMethod.invoke(ReflectionBasedServiceMethod.java:34)
        at org.gradle.internal.service.DefaultServiceRegistry$FactoryMethodService.invokeMethod(DefaultServiceRegistry.java:899)
        ... 86 more
Caused by: org.gradle.internal.service.ServiceLookupException: Could not configure services using GradleScopeCompileServices.configure().
        at org.gradle.internal.service.DefaultServiceRegistry.applyConfigureMethod(DefaultServiceRegistry.java:205)
        at org.gradle.internal.service.DefaultServiceRegistry.findProviderMethods(DefaultServiceRegistry.java:181)
        at org.gradle.internal.service.DefaultServiceRegistry.addProvider(DefaultServiceRegistry.java:265)
        at org.gradle.internal.service.DefaultServiceRegistry$1.addProvider(DefaultServiceRegistry.java:239)
        at org.gradle.api.internal.tasks.CompileServices.registerGradleServices(CompileServices.java:44)
        at org.gradle.internal.service.scopes.GradleScopeServices.lambda$new$0(GradleScopeServices.java:95)
        at org.gradle.internal.service.DefaultServiceRegistry.register(DefaultServiceRegistry.java:216)
        at org.gradle.internal.service.scopes.GradleScopeServices.<init>(GradleScopeServices.java:92)
        at org.gradle.internal.service.scopes.BuildScopeServiceRegistryFactory.createFor(BuildScopeServiceRegistryFactory.java:36)
        at org.gradle.invocation.DefaultGradle.<init>(DefaultGradle.java:99)
        at org.gradle.invocation.DefaultGradle_Decorated.<init>(Unknown Source)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at org.gradle.internal.instantiation.generator.AsmBackedClassGenerator$InvokeConstructorStrategy.newInstance(AsmBackedClassGenerator.java:1948)
        at org.gradle.internal.instantiation.generator.AbstractClassGenerator$GeneratedClassImpl$GeneratedConstructorImpl.lambda$newInstance$0(AbstractClassGenerator.java:510)
        at org.gradle.internal.deprecation.DeprecationLogger$4.create(DeprecationLogger.java:295)
        at org.gradle.internal.deprecation.DeprecationLogger.whileDisabledThrowing(DeprecationLogger.java:273)
        at org.gradle.internal.instantiation.generator.AbstractClassGenerator$GeneratedClassImpl$GeneratedConstructorImpl.newInstance(AbstractClassGenerator.java:509)
        at org.gradle.internal.instantiation.generator.DependencyInjectingInstantiator.doCreate(DependencyInjectingInstantiator.java:64)
        ... 94 more
Caused by: java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$PlatformClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$PlatformClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')
        at org.gradle.initialization.DefaultJdkToolsInitializer.initializeJdkTools(DefaultJdkToolsInitializer.java:41)
        at org.gradle.api.internal.tasks.CompileServices$GradleScopeCompileServices.configure(CompileServices.java:55)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:125)
        at org.gradle.internal.service.ReflectionBasedServiceMethod.invoke(ReflectionBasedServiceMethod.java:34)
        at org.gradle.internal.service.DefaultServiceRegistry.applyConfigureMethod(DefaultServiceRegistry.java:203)
        ... 113 more


* Get more help at https://help.gradle.org

BUILD FAILED in 3s

I’m using Java 17 and gradle 8, i feel it’s a gradle-wrapper error, but not sure…

Any help is appreciated.

Besides that you should not use gradle to run any build, but always the Gradle wrapper of that project (gradlew) so that the Gradle version the build was designed for is always used, what does running with --version or --show-version output?

I tried it with gradlew first, it gave the same error, since i was using ide’s like intelliJ and VSCode, gradlew version was 7.6 and gradle version was 8.0.2, regardless i doubt it’s a version error since both gave the same error. Could the downloading and extracting be the error?

Please do as I advised.
I’m not particularly interested in the Gradle version.

I get this response

Ok, thanks.

So I had a closer look at the stacktrace you posted.
But that is pretty much confusing.

In Java 9 there were many changes.

One change was, that PlatformClassLoader does not implement URLClassLoader anymore (it is an internal class anyway). Your stacktrace says that Gradle is trying nevertheless to cast the former to the latter and of course fails.

Another change in Java 9 was, that it does not contain a tools.jar anymore.
Actually Gradle checks whether a tools.jar is available in the JDK used to run Gradle and if it finds one, only then does that cast to inject the tools.jar into the system class loader.

So the code that fails for you should not actually run, as tools.jar is not present on a Java 17.

Does your Java installation that is used there come with a tools.jar in the lib folder?
Or did you put a tools.jar there yourself?

Either way, you should probably open a bug report for Gradle to handle the case that there is a tools.jar found but the class loader not being of the correct type more gracefully.

2 Likes

OMG Björn! You are an absolute legend!!! Yes it was the tools.jar that was causing the error, actually I was running the whole thing on my work laptop, and intelliJ was given preinstalled for us, it was probably some old version, and you know like, after spring boot 3 you are only allowed to use java 17 right… so i was using java 17, but on intelliJ i used to get the error that tools.jar is missing, so i had added that in the jdk. It worked for maven built projects, but gradle i guess got mad. But honestly, thank you so much, my senior at work with 20 years of experience said he couldn’t help me, even tho he had a look at my laptop, but you without even taking a look, just saw the error and gave possible solutions. ChatGpt was also dumbfounded when i pasted this error to it. I hope one day I become as good as you.

You should practically never add a tools.jar anywhere manually.
If you get a message that it is missing, that means what you use there needs a JDK of an older Java version.
Before Java 9, the tools.jar - which contained things like the Java compiler - were included in the JDK but not in the JRE.
Some tools that need the things from tools.jar checked for its existence and complained if it was missing, usually meaning you used a JRE instead of JDK.

If some tool complains about missing tools.jar with Java 9 or newer, it just means you need to use a newer version of that tool, or an older version of Java. Providing a tools.jar manually is not really the best idea, because it then also does not match version-wise.

Nevertheless you should open a bug report with Gradle to handle this situation more gracefully.
It should not fail to start up just because someone throws a file named tools.jar in there, or if it fails to start up, it should at least fail with a more meaningful message.

Hey Bjorn, so I reported the bug, so… since i like partially know the solution, should I add a custom error message or is reporting the bug more than enough. Also if i have to add the custom message, how do i go about doing that…?

To begin with, it is Björn or Bjoern, not Bjorn. :wink:

Great that you reported it, you should also link it here for others to easily find it if they read this thread.

If you also want to fix it, that’s even better, if a PR exists, it is more likely it will get fixed. At gradle/CONTRIBUTING.md at master · gradle/gradle · GitHub you find a documentation about contributing.