为什么我找不到包裹错误?
我创建了两个Java文件a.java
和b.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:
My directory structure after compilation of A.java
:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的项目目录看起来像这样:
您应该编译
a
这样的内容:和
b
,您应该从项目目录中执行此操作。编译后,目录结构看起来像这样:
顺便说一句,您的代码中有一个语法错误,应为
obj.printer();
而不是a.printer(); 。
Assuming your project directory looks like this:
you should compile
A
like this:and
B
withyou should do this from the project directory. After you compile, the directory structure looks like this:
BTW, you have a syntax error in your code, should be
obj.printer();
instead ofA.printer();
.无法从类型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.