5959import org .netbeans .modules .gradle .ProjectTrust ;
6060import org .netbeans .modules .gradle .api .GradleProjects ;
6161import org .netbeans .modules .gradle .api .NbGradleProject ;
62- import org .netbeans .modules .gradle .api .NbGradleProject .LoadOptions ;
6362import org .netbeans .modules .gradle .api .NbGradleProject .Quality ;
6463import org .netbeans .modules .gradle .execute .EscapeProcessingOutputStream ;
6564import org .netbeans .modules .gradle .execute .GradlePlainEscapeProcessor ;
65+ import org .netbeans .modules .gradle .options .GradleExperimentalSettings ;
6666import org .netbeans .modules .gradle .spi .GradleSettings ;
67+ import org .netbeans .modules .gradle .spi .execute .JavaRuntimeManager ;
6768import org .openide .loaders .DataFolder ;
6869import org .openide .loaders .DataObject ;
6970import org .openide .util .Exceptions ;
@@ -312,6 +313,8 @@ public String getMessage() {
312313 @ Override
313314 public Set <FileObject > execute () {
314315 GradleConnector gconn = GradleConnector .newConnector ();
316+ JavaRuntimeManager .JavaRuntime defaultRuntime = GradleExperimentalSettings .getDefault ().getDefaultJavaRuntime ();
317+
315318 target .mkdirs ();
316319 InputOutput io = IOProvider .getDefault ().getIO (projectName + " (init)" , true );
317320 try (ProjectConnection pconn = gconn .forProjectDirectory (target ).connect ()) {
@@ -359,7 +362,8 @@ public Set<FileObject> execute() {
359362 OutputStream out = new EscapeProcessingOutputStream (new GradlePlainEscapeProcessor (io , false ));
360363 OutputStream err = new EscapeProcessingOutputStream (new GradlePlainEscapeProcessor (io , false ))
361364 ) {
362- BuildLauncher gradleInit = pconn .newBuild ().forTasks (args .toArray (new String [0 ]));
365+ BuildLauncher gradleInit = pconn .newBuild ().forTasks (args .toArray (String []::new ));
366+ gradleInit .setJavaHome (defaultRuntime .getJavaHome ());
363367 if (GradleSettings .getDefault ().isOffline ()) {
364368 gradleInit = gradleInit .withArguments ("--offline" );
365369 }
@@ -370,7 +374,7 @@ public Set<FileObject> execute() {
370374 } catch (IOException iox ) {
371375 }
372376 } catch (GradleConnectionException | IllegalStateException ex ) {
373- Exceptions .printStackTrace (ex );
377+ ex .printStackTrace (io . getErr () );
374378 } finally {
375379 if (io .getOut () != null ) io .getOut ().close ();
376380 if (io .getErr () != null ) io .getErr ().close ();
@@ -444,6 +448,7 @@ public Set<FileObject> execute() {
444448 FileUtil .createFolder (dir );
445449 Thread .sleep (200 );
446450 } catch (InterruptedException | IOException ex ) {
451+ Exceptions .printStackTrace (ex );
447452 }
448453 return null ;
449454 }
@@ -479,6 +484,7 @@ public final Set<FileObject> execute() {
479484 }
480485
481486 } catch (IOException ex ) {
487+ Exceptions .printStackTrace (ex );
482488 }
483489 }
484490 return Set .of ();
@@ -542,6 +548,7 @@ public Set<FileObject> execute() {
542548 return ret ;
543549 }
544550 } catch (IOException | IllegalArgumentException ex ) {
551+ Exceptions .printStackTrace (ex );
545552 }
546553 }
547554 return null ;
@@ -568,21 +575,24 @@ public String getMessage() {
568575 @ Override
569576 public Set <FileObject > execute () {
570577 GradleConnector gconn = GradleConnector .newConnector ();
578+ JavaRuntimeManager .JavaRuntime defaultRuntime = GradleExperimentalSettings .getDefault ().getDefaultJavaRuntime ();
571579 try (ProjectConnection pconn = gconn .forProjectDirectory (projectDir ).connect ()) {
572580 List <String > args = new ArrayList <>();
573581 args .add ("wrapper" ); //NOI18N
574582 if (version != null ) {
575583 args .add ("--gradle-version" ); //NOI18N
576584 args .add (version );
577585 }
586+ BuildLauncher init = pconn .newBuild ()
587+ .setJavaHome (defaultRuntime .getJavaHome ());
578588 if (GradleSettings .getDefault ().isOffline ()) {
579- pconn .newBuild ().withArguments ("--offline" ).forTasks (args .toArray (new String [0 ])).run (); //NOI18N
580- } else {
581- pconn .newBuild ().forTasks (args .toArray (new String [0 ])).run ();
589+ init = init .withArguments ("--offline" );
582590 }
591+ init .forTasks (args .toArray (String []::new )).run ();
583592 } catch (GradleConnectionException | IllegalStateException ex ) {
584593 // Well for some reason we were not able to load Gradle.
585594 // Ignoring that for now
595+ Exceptions .printStackTrace (ex );
586596 }
587597 gconn .disconnect ();
588598 return null ;
@@ -684,7 +694,7 @@ public Set<FileObject> execute() {
684694 DataObject newData = o .createFromTemplate (targetFolder , targetName , tokens );
685695 return important ? Set .of (newData .getPrimaryFile ()) : null ;
686696 } catch (IOException ex ) {
687-
697+ Exceptions . printStackTrace ( ex );
688698 }
689699 }
690700 return null ;
0 commit comments