为什么我找不到包裹错误?

发布于 2025-02-10 01:06:39 字数 1103 浏览 2 评论 0原文

我创建了两个Java文件a.javab.java。这两个类都在同一文件夹testjava中。

a.java中的代码

package pkg;

public class A{
    int data;
    public void printer(){
        System.out.println("I'm in A");
    }
}

b.java中的代码

package mypkg;
import pkg.*;

public class B{
    void printer(){
        System.out.println("I'm in B");
    }
    public static void main(String[] args){
        A obj = new A();
        obj.printer();      
    }
}

编译我使用的第一个文件: Javac -d。 A.Java ./ pkg文件夹中的a.class a.class a.class 文件夹,该文件> a.class 文件夹,该文件> a.class forder 文件夹
要编译第二个文件,我正在使用javac -cp“ ./pkg” b.java,它给了我以下错误: “

我的目录结构a.java“

在我的class> classpath之后应包含什么?我已经阅读并尝试了有关同一主题的其他Stackoverflow问题,但无法解决我的问题。

I have created two java files A.java and B.java. Both classes are in same folder testjava.

Code in A.java

package pkg;

public class A{
    int data;
    public void printer(){
        System.out.println("I'm in A");
    }
}

Code in B.java

package mypkg;
import pkg.*;

public class B{
    void printer(){
        System.out.println("I'm in B");
    }
    public static void main(String[] args){
        A obj = new A();
        obj.printer();      
    }
}

To compile first file I used:
javac -d . A.java
which compiled with no errors and created A.class in ./pkg folder

To compile second file I am using javac -cp "./pkg" B.java which gives me the following errors:
Error Image

My directory structure after compilation of A.java:
After A.java compilation

What should include as my classpath? I have read and tried other StackOverflow questions on the same topic but couldn't solve my problem.

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

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

发布评论

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

评论(2

清泪尽 2025-02-17 01:06:39

假设您的项目目录看起来像这样:

project| tree
.
├── mypkg
│   └── B.java
└── pkg
    └── A.java

您应该编译a这样的内容:

javac pkg/A.java

b

javac mypkg/B.java

您应该从项目目录中执行此操作。编译后,目录结构看起来像这样:

project| tree
.
├── mypkg
│   ├── B.class
│   └── B.java
└── pkg
    ├── A.class
    └── A.java

顺便说一句,您的代码中有一个语法错误,应为obj.printer();而不是a.printer(); 。

Assuming your project directory looks like this:

project| tree
.
├── mypkg
│   └── B.java
└── pkg
    └── A.java

you should compile A like this:

javac pkg/A.java

and B with

javac mypkg/B.java

you should do this from the project directory. After you compile, the directory structure looks like this:

project| tree
.
├── mypkg
│   ├── B.class
│   └── B.java
└── pkg
    ├── A.class
    └── A.java

BTW, you have a syntax error in your code, should be obj.printer(); instead of A.printer();.

山田美奈子 2025-02-17 01:06:39

无法从类型A中静态引用非静态方法打印机()。
您从静态区域调用实例方法“打印机()”。

Cannot make a static reference to the non-static method printer() from the type A.
You are calling the instance method "printer()" from the static area.

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