You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetBeans运行Java项目时无法找到Main类问题求助

package com.mycompany.example;

import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

class Fruits {
    String name;
    double price;
    char quality;
    
    Fruits() {
        // Parameterless constructor
    }
    
    Fruits(String name, double price, char quality) {
        this.name = name;
        this.price = price;
        this.quality = quality;
    }
    
    Fruits(Fruits fruit) {
        this.name = fruit.name;
        this.price = fruit.price;
        this.quality = fruit.quality;
    }
    
    @Override
    public String toString() {
        return "Name: " + name + ", Price: " + price + ", Quality: " + quality;
    }
}

class Dehydrated extends Fruits {
    int quantity;
    boolean inTray;
    
    Dehydrated() {
        // Parameterless constructor
    }
    
    Dehydrated(String name, double price, char quality, int quantity, boolean inTray) {
        super(name, price, quality);
        this.quantity = quantity;
        this.inTray = inTray;
    }
    
    Dehydrated(Dehydrated dehydrated) {
        super(dehydrated);
        this.quantity = dehydrated.quantity;
        this.inTray = dehydrated.inTray;
    }
    
    @Override
    public String toString() {
        return super.toString() + ", Quantity: " + quantity + ", In Tray: " + inTray;
    }
    
    void state() {
        if (inTray) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter name: ");
            name = scanner.nextLine();
            System.out.print("Enter quality: ");
            quality = scanner.next().charAt(0);
        } else {
            System.out.println("State is only available for fruits in the tray.");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating arrays for each class
        Fruits[] fruitsArray = new Fruits[3];
        Dehydrated[] dehydratedArray = new Dehydrated[3];
        
        // Instantiating objects for the Fruits class using the three constructors
        fruitsArray[0] = new Fruits();
        fruitsArray[1] = new Fruits("Oranges", 4.5, 'B');
        fruitsArray[2] = new Fruits(fruitsArray[1]);
        
        // Instantiating objects for the Dehydrated class using the three constructors
        dehydratedArray[0] = new Dehydrated();
        dehydratedArray[1] = new Dehydrated("Apricots", 7.2, 'A', 20, true);
        dehydratedArray[2] = new Dehydrated(dehydratedArray[1]);
        
        // Calling the state() method for some instances of the Dehydrated class
        dehydratedArray[1].state(); // Will read from keyboard and modify
        dehydratedArray[2].state(); // Will not modify
        
        // Writing the modified instance results to a text file
        try {
            FileWriter writer = new FileWriter("results.txt");
            for (Dehydrated dehydrated : dehydratedArray) {
                writer.write(describeChanges(dehydrated));
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // Testing the created methods
        testMethods(fruitsArray, dehydratedArray);
    }
    
    // Method that describes changes for an instance of Dehydrated
    private static String describeChanges(Dehydrated dehydrated) {
        if (dehydrated.inTray) {
            return "Name: " + dehydrated.name + ", Quality: " + dehydrated.quality + "\n";
        } else {
            return ""; // If not in tray, no changes are made
        }
    }
    
    // Method for testing the created methods
    private static void testMethods(Fruits[] fruitsArray, Dehydrated[] dehydratedArray) {
        // Testing display methods
        for (Fruits fruit : fruitsArray) {
            System.out.println(fruit);
        }
        for (Dehydrated dehydrated : dehydratedArray) {
            System.out.println(dehydrated);
        }
        
        // Testing the state() method
        for (Dehydrated dehydrated : dehydratedArray) {
            dehydrated.state();
        }
    }
}
排查及解决NetBeans中"Main class not found / available"错误的建议
  • 强制重新编译项目
    右键项目选择「清理并构建」,彻底清除旧编译缓存和class文件,重新生成编译产物。之后检查项目目录/build/classes/com/mycompany/example/下是否存在Main.class文件——如果没有,说明代码存在编译错误,这是主类找不到的核心原因。

  • 修正代码语法错误
    你提供的代码里有HTML转义字符",Java编译器无法识别,必须替换为标准双引号"。替换后重新编译,否则会直接编译失败,无法生成主类的class文件。

  • 确认主类配置正确性
    右键项目→属性→运行,检查「主类」是否选中com.mycompany.example.Main。如果未选中或选错,点击右侧「浏览」按钮,手动定位选择该类。

  • 验证文件路径与包声明匹配
    确保Main.java实际存放路径是src/com/mycompany/example/,Java要求文件路径必须和包声明完全对应,否则编译器无法正确识别类的归属包。

  • 检查JDK配置有效性
    右键项目→属性→库,确认项目使用的JDK版本已安装且路径未变动。可以尝试切换到另一个可用JDK版本,再重新构建项目。

  • 重置NetBeans缓存
    关闭NetBeans,找到缓存目录(一般在用户目录/.netbeans/对应版本号/var/cache),删除其中所有内容,重启NetBeans后重新加载项目并构建。

内容的提问来源于stack exchange,提问作者Tosi Rom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 08:32:38