2222import java .io .IOException ;
2323import java .io .StringReader ;
2424import java .net .URL ;
25- import java .util .ArrayList ;
2625import java .util .Arrays ;
2726import java .util .Collections ;
2827import java .util .HashMap ;
3130import java .util .Scanner ;
3231import java .util .Set ;
3332import java .util .UUID ;
33+ import java .util .regex .Pattern ;
3434
3535import javax .swing .JEditorPane ;
3636import javax .swing .text .DefaultStyledDocument ;
@@ -421,20 +421,9 @@ private void addBundleJre() {
421421 throw new GradleException ( "No installed Java VMs found: " + java );
422422 }
423423
424- List <File > versions = new ArrayList <File >();
425- String [] s = { "jre" , "jdk" };
426- Arrays .asList ( s ).forEach ( prefix -> {
427- versions .addAll ( getDirectories ( java , prefix + jre ) );
428- if ( versions .size () == 0 ) {
429- // if the java version is like "1.8" then search also for "jre8"
430- String jreStr = jre .toString ();
431- if ( jreStr .length () > 2 && jreStr .startsWith ( "1." ) ) {
432- versions .addAll ( getDirectories ( java , "jre" + jreStr .substring ( 2 ) ) );
433- }
434- }
435- });
424+ List <File > versions = getMatchingJREs (java , jre .toString ());
436425
437- if ( versions .size () == 0 ) {
426+ if ( versions .isEmpty () ) {
438427 throw new GradleException ( "bundleJre version " + jre + " can not be found in: '" + java + "' Its search for an folder that starts with: jre" + jre );
439428 }
440429
@@ -456,22 +445,23 @@ private void addBundleJre() {
456445
457446 addDirectory ( jreDir , baseLength , javaDir );
458447 }
459-
448+
460449 /**
461- * Get all directories from the directory that start with the prefix.
462- *
463- * @param parent parent directory
464- * @param prefix the searching prefix
465- * @return list of directories
450+ * Gets all matching jres in the specified folder
451+ * @param parent the folder to search in
452+ * @param jre the jre string to match
453+ * @return returns a list of matching jre folders
466454 */
467- private static List <File > getDirectories ( File parent , String prefix ) {
468- ArrayList <File > files = new ArrayList <File >();
469- for ( File file : parent .listFiles () ) {
470- if ( file .isDirectory () && file .getName ().startsWith ( prefix ) ) {
471- files .add ( file );
472- }
455+ private static List <File > getMatchingJREs (File parent , String jre ) {
456+ // Always remove "1." from the version number. It is optionally included in the pattern below
457+ if (jre .length () > 2 && jre .startsWith ( "1." )) {
458+ jre = jre .substring (2 );
473459 }
474- return files ;
460+ Pattern jreMatcher = Pattern .compile ("\\ Aj(re|dk)-?(1\\ .)?" +Pattern .quote (jre ));
461+ List <File > versions = Arrays .asList (
462+ parent .listFiles (file ->file .isDirectory () && jreMatcher .matcher (file .getName ()).find ())
463+ );
464+ return versions ;
475465 }
476466
477467 /**
0 commit comments