libGDX搭建Mario项目遇错求助:Java版本识别失败及类找不到问题
Hey there! Let's work through those two issues you're hitting with your libGDX Mario project—since you're only two months into Java and following a 2015 tutorial, version mismatches and configuration hiccups are totally expected, so don't stress!
This error happens because the 2015-era libGDX tooling (like Gradle) wasn't built to recognize newer Java version number formats (Java 9+ switched from the old 1.x naming to just x). Here are two solid fixes:
- Downgrade to Java 8: This is the most straightforward fix, since Java 8 was the standard when the tutorial was released, and early libGDX versions work flawlessly with it. Install Java 8, then go into your IDE's project settings and set the SDK to Java 8 for all modules in your project.
- Update Gradle config (if you want to keep Java 9+): If you'd rather stick with your current Java version, tweak your project's root
build.gradlefile:
Also, update your Gradle wrapper to a version that supports newer Java versions. Openallprojects { apply plugin: "java" sourceCompatibility = 1.8 targetCompatibility = 1.8 }gradle/wrapper/gradle-wrapper.propertiesand change the distribution URL to:
This version of Gradle works with Java 8 through 11, which should cover your Java 9.0.1 setup.distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-all.zip
This usually boils down to a misconfigured project structure or IDE sync issue. Try these steps:
- Double-check your file structure: Make sure
DesktopLauncher.javais located exactly atsrc/desktop/java/com/mygdx/mario/desktop/DesktopLauncher.java. Also verify the package declaration at the top of the file ispackage com.mygdx.mario.desktop;—Java is case-sensitive, so even a tiny typo (likeDesktoplauncherinstead ofDesktopLauncher) will break this. - Re-sync your project with Gradle: If you're using IntelliJ or Eclipse, use the IDE's Gradle sync feature (in IntelliJ, it's the elephant icon in the top-right). Sometimes IDEs fail to pick up new classes or module configurations until you sync.
- Verify the desktop module's build.gradle: Open the
desktop/build.gradlefile and make sure the source sets are correctly pointing to your code:sourceSets.main.java.srcDirs = ["src/desktop/java/"] - Run a Gradle compile command: Open your terminal, navigate to your project root, and run
./gradlew desktop:compileJava(on Windows, usegradlew.bat desktop:compileJava). This will show you any actual compilation errors that might be hiding behind the "class not found" message—maybe a missing import or syntax error that's preventing the class from being built.
You're doing great for only two months into Java—these setup kinks are just part of learning how game dev tools work. Stick with it, and you'll have your Mario clone up and running in no time!
内容的提问来源于stack exchange,提问作者Signka

