本文介绍了在java中导入类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个主目录(包含main.java)和一个子目录(包含child.java)。
I have a main directory (contains main.java) and a subdirectory( contains child.java).
我的问题是如何在main.java中实例化child.java
My problem is how to instantiate child.java in main.java
- 我让孩子上课了。 &安培;将第1行添加为
包mypackage
- 我编译了
child.class
使用javac -d。 child.java
创建一个新的mypackage目录。 - 我试图在main中导入子类,如下所示:
import subdirectory.mypackage。 *
(注意-d选项将child.class放在mypackage文件夹中) - 我使用javac main.java编译了main.java文件
- I have made the child class public. & added the line#1 as
package mypackage
- I have compiled
child.class
withjavac -d . child.java
which creates a new mypackage directory. - I tried to import child class in main as follows:
import subdirectory.mypackage.*
(note -d option places the child.class inside mypackage folder) - I compiled the main.java file with "javac main.java"
我收到以下错误:
mainAESE.java:9: cannot access subdirectory.child
bad class file: RegularFileObject[./subdirectory/child
class file contains wrong class: mypackage.child
Please remove or make sure it appears in the correct subdirectory of the class
child childInstance= new child();
^
1 error
请帮助我!!
推荐答案
确保包文件夹 mypackage
和 Main.class
分享父文件夹。
Be ensure that the package folder mypackage
and Main.class
share the parent folder.
package mypackage;
public class Child {}
我认为 Main
类在默认
包中创建。
I presume that the Main
class is created in default
package.
public class Main {
public static void main(String []args){
mypackage.Child child=new mypackage.Child();
}
}
您的目录结构应为:
main-directory/
|
|----/mypackage/
Child.class
|
| Main.class
| Main.java
| Child.java
并启动/加载 Main
发出以下命令,
and to launch/load the Main
issue following command,
这篇关于在java中导入类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!