linux-china/toolchains-maven-plugin

Getting IndexOutOfBoundsException when requesting JDK 8

helpermethod opened this issue · 5 comments

Hi!

With the following configuration I'm getting an IndexOutOfBoundsException. I suppose there is some problem with the download?

<plugin>
    <groupId>org.mvnsearch</groupId>
    <artifactId>toolchains-maven-plugin</artifactId>
    <version>4.5.0</version>
    <executions>
        <execution>
            <goals>
                <goal>toolchain</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <toolchains>
            <jdk>
                <version>8</version>
            </jdk>
            <testJdk>
                <version>17</version>
            </testJdk>
        </toolchains>
    </configuration>
</plugin>
[INFO] --- toolchains-maven-plugin:4.5.0:toolchain (default-cli) @ zip-mold ---
[INFO] Required toolchain: jdk [ version='8' ]
[INFO] Begin to install JDK 8
[ERROR] Failed to download and install JDK
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
    at jdk.internal.util.Preconditions.outOfBounds (Preconditions.java:100)
    at jdk.internal.util.Preconditions.outOfBoundsCheckIndex (Preconditions.java:106)
    at jdk.internal.util.Preconditions.checkIndex (Preconditions.java:302)
    at java.util.Objects.checkIndex (Objects.java:359)
    at java.util.ArrayList.get (ArrayList.java:427)
    at com.google.gson.JsonArray.get (JsonArray.java:203)
    at org.apache.maven.plugins.toolchain.FoojayService.parseFileNameAndDownloadUrl (FoojayService.java:145)
    at org.apache.maven.plugins.toolchain.FoojayService.downloadAndExtractJdk (FoojayService.java:82)
    at org.apache.maven.plugins.toolchain.ToolchainMojo.autoInstallJdk (ToolchainMojo.java:233)
    at org.apache.maven.plugins.toolchain.ToolchainMojo.selectToolchain (ToolchainMojo.java:195)
    at org.apache.maven.plugins.toolchain.ToolchainMojo.execute (ToolchainMojo.java:111)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
    at java.lang.reflect.Method.invoke (Method.java:577)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
    at org.codehaus.classworlds.Launcher.main (Launcher.java:47)
[ERROR] No toolchain matched from 1 found for toolchainType jdk
dsyer commented

Works for me. Maybe there's a proxy hiding the foojay service or something in your environment. What do you see if you visit https://api.foojay.io/disco/v3.0 on the command line and in browser?

@helpermethod yes, could you check your proxy? The error is from following code, and almost json data problem caused by proxy.

String queryUrl = "https://api.foojay.io/disco/v3.0/packages?"
                + "distribution=" + vendor
                + "&version=" + version
                + "&operating_system=" + os
                + "&architecture=" + archName
                + "&bitness=" + bitness
                + "&archive_type=" + archiveType
                + "&libc_type=" + libcType
                + "&latest=overall&package_type=jdk&discovery_scope_id=directly_downloadable&match=any&javafx_bundled=false&directly_downloadable=true&release_status=ga";
        HttpGet request = new HttpGet(queryUrl);
        final HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == 200) {
            Gson gson = new Gson();
            final JsonObject jsonObject = gson.fromJson(EntityUtils.toString(response.getEntity()), JsonElement.class).getAsJsonObject();
            final JsonObject pkgJson = jsonObject.getAsJsonArray("result").get(0).getAsJsonObject();
            String pkgInfoUri = pkgJson.getAsJsonObject("links").get("pkg_info_uri").getAsString();
            HttpGet pkgInfoGet = new HttpGet(pkgInfoUri);
            final HttpResponse pkgInfoResponse = httpClient.execute(pkgInfoGet);
            if (pkgInfoResponse.getStatusLine().getStatusCode() == 200) {
                final JsonObject pkgInfoJson = gson.fromJson(EntityUtils.toString(pkgInfoResponse.getEntity()), JsonElement.class).getAsJsonObject();
                String downloadUrl = pkgInfoJson.getAsJsonArray("result").get(0).getAsJsonObject().get("direct_download_uri").getAsString();
                return new String[]{pkgJson.get("filename").getAsString(), downloadUrl};
            }
        }

I will take a look at the code and make good logging for error.

Works for me. Maybe there's a proxy hiding the foojay service or something in your environment. What do you see if you visit https://api.foojay.io/disco/v3.0 on the command line and in browser?

Works fine for me:

Browser:

image

CLI:

image

If I switch to Temurin it works too:

<plugin>
    <groupId>org.mvnsearch</groupId>
    <artifactId>toolchains-maven-plugin</artifactId>
    <version>4.5.0</version>
    <executions>
        <execution>
            <goals>
                <goal>toolchain</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <toolchains>
            <jdk>
                <vendor>temurin</vendor>
                <version>8</version>
            </jdk>
            <testJdk>
                <vendor>temurin</vendor>
                <version>17</version>
            </testJdk>
        </toolchains>
    </configuration>
</plugin>

@dsyer @helpermethod a question here. Now the default vendor is oracle_open_jdk, and it should be replaced by temurin? Some versions, such as 12, 13, 14, 15, 16 not available from temurin.

            String vendor = params.get("vendor");
            if (vendor == null || vendor.isEmpty()) {
                vendor = "oracle_open_jdk";
            }

Snip20230420_43

I actually don't care about the vendor, as long as it works 😆. I wonder why I gut an IndexOutOfBoundsException. It may be related to my Arch/OS. I'll try to dig deeper.