FIXED: BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 65

So I decided to try my hand and Forge Minecraft modding, not knowing fully what I’m doing.
At the first step of configuring VSCode I encountered an error: BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 65
Now after looking, I found that it was because my Java version was 22, now Gradle 7.5.1 only supports up to version 18. But what I didn’t find was how to fix it if you have multiple java installs (and I was building my project on a drive that wasn’t the ‘C’ drive).

So I decided to take a look at the gradlew file, and low and behold, I figured out how to force set the Java install.
The code that determines the Java command to use is:

# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
        # IBM's JDK on AIX uses strange locations for the executables
        JAVACMD=$JAVA_HOME/jre/sh/java
    else
        JAVACMD=$JAVA_HOME/bin/java
        echo "$JAVACMD"
    fi
    if [ ! -x "$JAVACMD" ] ; then
        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
else
    JAVACMD=java
    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

So I set the JAVACMD variable to $PWD/java/openjdk-18_windows-x64_bin/jdk-18/bin/java
So the script would now look like this:

# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
        # IBM's JDK on AIX uses strange locations for the executables
        JAVACMD=$JAVA_HOME/jre/sh/java
    else
        #JAVACMD=$JAVA_HOME/bin/java #Dont use as java is too new
        JAVACMD=$PWD/java/openjdk-18_windows-x64_bin/jdk-18/bin/java
        echo "$JAVACMD"
    fi
    if [ ! -x "$JAVACMD" ] ; then
        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
else
    JAVACMD=java
    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

You would not change the gradlew script, but simply set your JAVA_HOME environment variable.

But this is only relevant when you start Gradle from the commandline.
If you are talking about running from VSC, it almost certainly does not use the gradlew scripts, but the tooling API, so you have to check how you cofigure the Java to use to run Gradle in VSC.