javac: file not found: polymorphism.music.Music.java or Error: Could not find or load main class pol

Java命令解析与常见错误
本文详细解析了Java命令的基本格式,包括如何指定最大内存、加载并运行特定类的方法,以及如何传递命令行参数。同时,针对常见的编译和运行时错误,如文件未找到、主类找不到等问题,提供了具体的解决步骤和建议。

An example of what a java command should look like:

    java -Xmx100m com.acme.example.ListUsers fred joe bert

The above is going to cause the java command to do the following:

  1. Search for the compiled version of the com.acme.example.ListUsers class.
  2. Load the class.
  3. Check that the class has a main method with signaturereturn type and modifiers given by public static void main(String[]). (Note, the method argument's name is NOT part of the signature.)
  4. Call that method passing it the command line arguments ("fred", "joe", "bert") as a String[].

Q1. javac: file not found: polymorphism.music.Music.java 

A1: instead use

$ javac polymorphism/music/Music.java

also use absolute classpath, for example,

javac ~/user/polymorphism/music/Music.java

Q2: Error: Could not find or load main class polymorphism.music.Music

A1: be sure already has Music.class, then use

$ java polymorphism.music.Music

or 

java polymorphism/music/Music

If you want use javac/java in current directory, you need modify source code. Remove package sentences and some comments in exmaples code.

refereces:

1. https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean 

这个错误通常是由于 **类路径(Classpath)** 或 **类名拼写** 问题导致的。以下是详细分析和解决方案: --- ### **错误原因** 1. **类名拼写错误** - 编译后的 `.class` 文件名必须与 `public class` 名称完全一致(区分大小写)。 - 例如:如果类定义为 `public class JNILoaderTest`,但运行时输入 `java jniloadertest`(小写),会报错。 2. **类路径(Classpath)问题** - 默认情况下,`java` 命令只在当前目录(`.`)下查找 `.class` 文件。 - 如果从其他目录运行,或未正确指定类路径,会导致找不到类。 3. **包(Package)声明问题** - 如果代码中有 `package` 声明(如 `package com.example;`),运行时需要指定完全限定类名(如 `java com.example.JNILoaderTest`)。 --- ### **解决方案** #### **1. 检查类名和文件名** - 确保 `JNILoaderTest.java` 中的类名是 `public class JNILoaderTest`(与文件名一致)。 - 编译后检查生成的 `.class` 文件: ```bash ls JNILoaderTest.class # 确认文件存在 ``` #### **2. 在正确目录下运行** - 如果从其他目录运行,需指定类路径: ```bash # 假设当前在项目根目录,编译后的 .class 文件在 ./bin 下 java -cp ./bin JNILoaderTest ``` #### **3. 处理包声明** - 如果代码中有 `package` 声明(如 `package test;`),需: 1. 保持目录结构匹配包名(如 `./test/JNILoaderTest.java`)。 2. 编译时指定输出目录: ```bash javac -d ./bin test/JNILoaderTest.java ``` 3. 运行时使用完全限定类名: ```bash java -cp ./bin test.JNILoaderTest ``` #### **4. 清理并重新编译** - 删除旧的 `.class` 文件并重新编译: ```bash rm JNILoaderTest.class javac JNILoaderTest.java java JNILoaderTest # 确保当前目录是 .class 文件所在目录 ``` --- ### **完整示例** #### **目录结构** ``` /project ├── JNILoaderTest.java └── (编译后生成) JNILoaderTest.class ``` #### **代码(JNILoaderTest.java)** ```java public class JNILoaderTest { public static void main(String[] args) { System.out.println("Test passed: Class loaded successfully!"); } } ``` #### **操作步骤** ```bash # 1. 进入代码所在目录 cd /project # 2. 编译(确保无编译错误) javac JNILoaderTest.java # 3. 运行(注意当前目录是类路径) java JNILoaderTest ``` #### **预期输出** ``` Test passed: Class loaded successfully! ``` --- ### **常见陷阱** 1. **大小写敏感** - Linux/Unix 系统下,`java JNILoaderTest` 和 `java jniloadertest` 是不同的。 2. **误用 `-cp` 参数** - 如果指定了 `-cp`,默认不会包含当前目录(`.`),需显式添加: ```bash java -cp .:other/paths JNILoaderTest # Linux/macOS java -cp .;other\paths JNILoaderTest # Windows(分号分隔) ``` 3. **IDE 生成的类路径问题** - 如果在 IDE(如 IntelliJ/Eclipse)中运行正常但命令行失败,检查 IDE 的运行配置中的类路径设置。 --- ### **扩展建议** - **使用构建工具**:推荐用 `Maven` 或 `Gradle` 管理类路径,避免手动编译/运行错误。 - **调试脚本**:编写简单的 Shell 脚本自动化测试: ```bash #!/bin/bash javac JNILoaderTest.java && java JNILoaderTest ``` --- ### **总结** - **根本原因**:`java` 命令未找到 `.class` 文件,通常是由于目录或类名不匹配。 - **解决步骤**: 1. 确认类名和文件名一致。 2. 在 `.class` 文件所在目录运行,或通过 `-cp` 指定路径。 3. 检查包声明和目录结构是否匹配。 - **关键命令**: ```bash javac JNILoaderTest.java && java -cp . JNILoaderTest ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值