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

Java运行报错:无法找到或加载主类myRunningCode求助

Fixing "Could not find or load main class" Error in Java

Hey there, I’ve run into this exact snag before—let’s break it down and fix it quickly!

The Root Cause

When you compiled your code with javac -cp "/temp/package.jar" myRunningCode.java, the compiler generated myRunningCode.class right in your current working directory. But when you ran java -cp "/temp/package.jar" myRunningCode, you only told the JVM to look inside the package.jar file for classes—it had no clue to check your current folder where your compiled class lives.

The Fix

You need to add your current directory to the classpath so the JVM can locate your myRunningCode.class file. The syntax varies a bit by operating system:

  • Linux/macOS: Use a colon (:) to separate classpath entries
    java -cp ".:/temp/package.jar" myRunningCode
    
  • Windows: Use a semicolon (;) instead (since colons are used for drive letters here)
    java -cp ".;/temp/package.jar" myRunningCode
    

Quick Note

The . in the classpath stands for your current working directory. By including it alongside the dependency jar, you’re telling the JVM: "Check both my current folder and the package.jar file to find all the classes I need to run this code."

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 08:47:47