如何在 .java 文件中导入 .class 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2477947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import .class file in a .java file?
提问by Namratha
What i need to do is as follows:
我需要做的是如下:
I have a bigloo scheme program (*.scm), then using the bigloo frameworks jvm a class file is generated.
I want to use this .class file from a .java file. That is, i need to import this(.class) file as i want to use some functions defined in the scheme file which is now a .class file.
How do i do it in Eclipse? i have created two packages one for java and one for the .class file. Then i import the package containing the .class file. But i am not able to use the function in the .class file in the .java file. Are there any settings to be done?
我有一个 bigloo 方案程序 (*.scm),然后使用 bigloo 框架 jvm 生成一个类文件。
我想使用 .java 文件中的这个 .class 文件。也就是说,我需要导入 this(.class) 文件,因为我想使用在现在是 .class 文件的方案文件中定义的一些函数。
我如何在 Eclipse 中做到这一点?我创建了两个包,一个用于 java,一个用于 .class 文件。然后我导入包含 .class 文件的包。但我无法在 .java 文件中的 .class 文件中使用该函数。有什么设置要做吗?
Please let me know how this can be done.
请让我知道如何做到这一点。
回答by Neal Donnan
You can add a folder containing compiled classes to your project by right clicking the project, then select Properties > Java build path, then "Add External Class Folder"
您可以通过右键单击项目,然后选择“属性”>“Java 构建路径”,然后选择“添加外部类文件夹”,将包含已编译类的文件夹添加到您的项目中
Or choose "Add Class Folder" if you have copied the class files into a sub directory of your project.
或者,如果您已将类文件复制到项目的子目录中,则选择“添加类文件夹”。
This will add the class files to the projects classpath and you can then import the class into your java file using an import statement:
这会将类文件添加到项目类路径中,然后您可以使用 import 语句将类导入到您的 java 文件中:
import my.package.MyClass;
Note:The package structure should be maintained under the folder that you add as a class folder. So if you add folder "myclasses" as a class folder, the directory structure should be as follows for the example above:
注意:包结构应保留在您添加为类文件夹的文件夹下。所以如果将文件夹“myclasses”添加为类文件夹,则上面示例的目录结构应如下所示:
myclasses/my/package/MyClass.class
myclasses/my/package/MyClass.class
回答by Chris Dennett
You could create a jar out of it and add it as a library -- jars are typically packaged with just the .class files. Just jam them into a .zip file with the correct directory structure and basic files (unzip another .jar to have a look inside) and rename to .jar. You could also use the official jar tool -- e.g., jar cvf TicTacToe.jar TicTacToe.class
您可以从中创建一个 jar 并将其添加为一个库——jar 通常只与 .class 文件一起打包。只需将它们塞入具有正确目录结构和基本文件的 .zip 文件中(解压缩另一个 .jar 以查看内部)并重命名为 .jar。你也可以使用官方的 jar 工具——例如,jar cvf TicTacToe.jar TicTacToe.class
http://java.sun.com/docs/books/tutorial/deployment/jar/build.html
http://java.sun.com/docs/books/tutorial/deployment/jar/build.html
(source: sun.com)
(来源:sun.com)