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

Java代码编译成功但多款IDE提示无运行入口问题排查

问题根因分析
  • 核心错误:Java标准程序入口public static void main(String[] args)的放置位置不符合规范
    你当前的main方法写在了静态内部类ExThread中,并没有放在公共顶层类project42的内部。IDE扫描可运行入口时,默认只会检索顶层公共类下的符合签名的main方法,因此无法识别到内部类里的main,就会提示没有可运行内容。
修复方案

把main方法从ExThread内部类中移到顶层类project42下即可,修复后的代码示例:

import java.util.concurrent.*;

//创建类
public class project42 {
    //主方法移到顶层类下
    public static void main(String[] args) throws InterruptedException {
        System.out.print("hi");
        Semaphore sema = new Semaphore(1);
        //创建车辆线程
        ExThread et1 = new ExThread(sema, "Westbound Cars");
        ExThread et2 = new ExThread(sema, "Eastbound Cars");
        //启动程序
        et1.start();
        et2.start();

        et1.join();
        et2.join();
    }

    static class ExThread extends Thread {
        //定义变量
        Semaphore sema;
        String theName;

        public ExThread(Semaphore sema, String theName) {
            super(theName);
            this.sema = sema;
            this.theName = theName;
        }

        public void run() {
            //测试西行车辆
            if (this.getName().equals("Westbound Cars")) {
                try {
                    System.out.println(theName + " are waiting");
                    sema.acquire();
                    System.out.println(theName + " are attempting");
                    for (int i = 1; i < 6; i++) {
                        System.out.println("Westbound: " + i + " is waiting");
                        Thread.sleep(1000);
                        System.out.println("Westbound: " + i + " has crossed");
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException exc) {
                    System.out.println(exc);
                }
                System.out.println(theName + " has crossed the bridge");
                sema.release();
            } else {
                //测试东行车辆
                System.out.println(theName + " are waiting");
                try {
                    System.out.println(theName + " are waiting");
                    sema.acquire();
                    System.out.println(theName + " are attempting");
                    for (int i = 1; i < 6; i++) {
                        System.out.println("Eastbound " + i + " is crossing");
                        Thread.sleep(1000);
                        System.out.println("Eastbound " + i + " has crossed");
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException exc) {
                    System.out.println(exc);
                }
                System.out.println(theName + " have crossed the bridge");
                sema.release();
            }
        }
    }
}
其他可修复的小问题
  • 原代码中第二个线程名多打了一个大写W,变成了WEastbound Cars,可修正为Eastbound Cars
  • 原代码中线程等待逻辑写了两次et1.join(),没有等待et2执行结束,建议修改为et2.join()
  • 原代码存在拼写错误Eastboud,可修正为Eastbound
  • 东行车辆的打印语句Eastbound " + i + "is crossing缺少空格,可调整为Eastbound " + i + " is crossing

内容的提问来源于stack exchange,提问作者Justin Joseph

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 06:57:03