尝试在PowerShell上运行Java文件时不存在软件包

发布于 2025-01-21 14:07:28 字数 1060 浏览 0 评论 0原文

我有一个名为PackageTest的Eclipse项目,其中有两个软件包,每个软件包都有一个。

这是两者的代码:

package test;
import apple.Apple;

public class Main {
    public static void main(String[] args) {
         Apple apple = new Apple(5);
    }
}

package apple;

public class Apple {
    
    private int value;
    
    public Apple(int value) {
        this.value = value;
    }
}

我可以从eclipse内部运行main.java,但是当我尝试使用时:

java (rest of directory)\PackageTest\src\test\Main.java

在命令行上,我收到此错误:

import apple.Apple;
            ^
Main.java:6: error: cannot find symbol
         Apple apple = new Apple(5);
         ^
  symbol:   class Apple
  location: class Main
Main.java:6: error: cannot find symbol
         Apple apple = new Apple(5);
                           ^
  symbol:   class Apple
  location: class Main
3 errors
error: compilation failed

是的,我尝试过

java (rest of directory)\PackageTest\src\test\Main.java --cp (rest of directory)\PackageTest

I have an eclipse project named PackageTest with two packages with one file inside each.: src/apple/Apple.java and src/test/Main.java.

Here is the code of both:

package test;
import apple.Apple;

public class Main {
    public static void main(String[] args) {
         Apple apple = new Apple(5);
    }
}

package apple;

public class Apple {
    
    private int value;
    
    public Apple(int value) {
        this.value = value;
    }
}

I can run Main.java just fine from inside eclipse, but when I try to use:

java (rest of directory)\PackageTest\src\test\Main.java

On the command line, I receive this error:

import apple.Apple;
            ^
Main.java:6: error: cannot find symbol
         Apple apple = new Apple(5);
         ^
  symbol:   class Apple
  location: class Main
Main.java:6: error: cannot find symbol
         Apple apple = new Apple(5);
                           ^
  symbol:   class Apple
  location: class Main
3 errors
error: compilation failed

And yes, I have tried

java (rest of directory)\PackageTest\src\test\Main.java --cp (rest of directory)\PackageTest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2025-01-28 14:07:28

Java源代码软件包是一个大型层次命名空间。
尝试更改为Apple类的文件夹结构。该项目需要一个用于Java类的主/Java文件夹。

src/ main/java /apple/apple.java

示例:

src/test/java/com/acme/foo/BarTest.java
src/main/java/com/acme/foo/Bar.java

问候。

The Java source code packages as one big hierarchical namespace.
Try change to folder structure for Apple Class. The project need a main/java folder for java class.

src/main/java/apple/Apple.java

Example:

src/test/java/com/acme/foo/BarTest.java
src/main/java/com/acme/foo/Bar.java

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文