MacOS下STS报错:无法找到主类(已尝试各类解决方案)
Hey there! Let's work through this frustrating issue together—it's super common for new programmers getting started with STS on Mac, so you're not alone. Here are the most likely fixes, ordered from easiest to check:
1. Double-check your main class structure
First, make sure your TestApp class has the exact correct main method signature and is declared as public. Even tiny typos can break things:
public class TestApp { public static void main(String[] args) { System.out.println("hello world"); } }
- Ensure the class is
public(required if it's in the default package for STS to detect it as a main class). - Don't misspell
main(e.g.,Mainwith a capital M) orString[] args(e.g., missing brackets, or writingarginstead ofargs).
2. Verify your Run Configuration
STS sometimes gets confused with saved run settings. Let's reset or check your configuration:
- Right-click your project → Run As → Run Configurations.
- Under Java Application, delete any existing configurations for your project if they exist.
- Click New Configuration → In the Main Class field, click Search and select
TestAppfrom the list. - Confirm the Project dropdown is set to your test project, then click Apply and Run.
3. Clean and rebuild your project
MacOS can leave cached build files that mess with STS's ability to find classes. Let's clear that:
- Go to the Project menu → Clean.
- Select your test project from the list, then click OK.
- After cleaning, go back to Project → Build All (or ensure "Build Automatically" is checked in the Project menu).
If you're using Maven, right-click the project → Maven → Update Project (check "Force Update of Snapshots/Releases" if needed) to refresh dependencies and build files.
4. Check your Java Build Path and JDK settings
STS on Mac often defaults to a JRE instead of a full JDK, which can cause class detection issues:
- Right-click your project → Properties → Java Build Path → Libraries tab.
- Look for "JRE System Library"—if it says "JRE" instead of "JDK", click Edit → select "Alternate JRE" → choose the 64-bit JDK you have installed.
- Next, go to Java Compiler in the project properties, and make sure the Compiler compliance level matches your JDK version (e.g., set to 17 if you're using JDK 17).
- Also, check for red crosses next to any libraries in the Build Path—remove and re-add any problematic ones.
5. Verify STS's global JDK configuration
Sometimes STS itself uses the wrong JDK, which affects all projects:
- Go to Eclipse → Preferences (from the top Mac menu bar).
- Navigate to Java → Installed JREs.
- Make sure your 64-bit JDK is listed and selected as the default. If it's missing, click Add → Standard VM → browse to your JDK installation folder (usually
/Library/Java/JavaVirtualMachines/).
Try these steps one by one—chances are one of these small tweaks will fix the issue. Don't hesitate to iterate if something doesn't work right away!
内容的提问来源于stack exchange,提问作者Nate Perkins

