Java 如何在 Eclipse 的项目中包含 .class 文件?(爪哇)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/661110/
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 do I include .class files in my project in Eclipse? (Java)
提问by jergason
Hey all. I am working on a project for school where we are given the .class file but not the source to include in our code. I am using Eclipse, and I want to include the file in my project so I can instantiate objects from it and use it.
大家好。我正在为学校开展一个项目,在该项目中我们获得了 .class 文件,但没有包含在我们的代码中的源代码。我正在使用 Eclipse,我想将该文件包含在我的项目中,以便我可以从中实例化对象并使用它。
The file is TokenizerImpl.class, and I want to use it like this:
该文件是 TokenizerImpl.class,我想像这样使用它:
TokenizerImpl tokenizer = new TokenizerImpl(foo);
TokenizerImpl tokenizer = new TokenizerImpl(foo);
I put the file in my project folder, and Eclipse says that "TokenizeImpl cannot be resolved as a type", which I assume means it cannot find the class or source. I tried putting it in the "bin" folder of the project and got the same error. Google search and SO search didn't seem to answer this, so I will give it a shot. How do I do this, oh wise ones?
我将该文件放在我的项目文件夹中,Eclipse 说“无法将 TokenizeImpl 解析为类型”,我认为这意味着它找不到类或源。我试着把它放在项目的“bin”文件夹中,但得到了同样的错误。谷歌搜索和 SO 搜索似乎没有回答这个问题,所以我会试一试。我该怎么做,哦,聪明的人?
Edit: Oh dear, I found the problem was something else entirely. These solutions worked fine, but I just forgot to create the Tokenizer interface that TokenizerImpl implements. Doh. Thanks for all your help though, I did learn a lot about eclipse.
编辑:哦,亲爱的,我发现问题完全是其他问题。这些解决方案运行良好,但我只是忘记创建 TokenizerImpl 实现的 Tokenizer 接口。多。感谢您的所有帮助,我确实学到了很多关于 eclipse 的知识。
采纳答案by Vineet Reynolds
You can add a directory containing the class files to the Eclipse project, only if it is inside one of your Eclipse projects, either in a generated directory or in one you have created.
您可以将包含类文件的目录添加到 Eclipse 项目中,前提是该目录位于您的 Eclipse 项目之一中,无论是在生成的目录中还是在您创建的目录中。
This can be done by adding the class folder to the Java build path of the application. You can set this in the Project properties, by visiting Java Build Path -> Libraries -> Add Class Folder. Keep in mind, that you will have to specify the root folder containing the class files in their packages.
这可以通过将类文件夹添加到应用程序的 Java 构建路径来完成。您可以通过访问 Java Build Path -> Libraries -> Add Class Folder 在项目属性中进行设置。请记住,您必须在其包中指定包含类文件的根文件夹。
Therefore, if you wish to have the compiler access com.stackoverflow.Example.class present in the classes directory under project A (but not in the build path of project A), then you should add 'classes' as a class folder, and not classes/com/stackoverflow as a class folder.
因此,如果您希望编译器访问 com.stackoverflow.Example.class 存在于项目 A 下的 classes 目录中(但不在项目 A 的构建路径中),那么您应该将“classes”添加为类文件夹,并且不是 classes/com/stackoverflow 作为类文件夹。
回答by Eddie
Other people have now given better answers. This "answer" was mainly to get information from the OP because the original question didn't really tell us, fully, what had been tried. There are now twoanswersthat truly answer the question in a long-term way.
其他人现在给出了更好的答案。这个“答案”主要是为了从 OP 获取信息,因为最初的问题并没有真正告诉我们已经尝试过的内容。现在有两个答案可以从长远角度真正回答这个问题。
My original answer is left below for context.
我的原始答案保留在下面以供参考。
Did you copy it to the bin folder within Eclipse or outside Eclipse? If you did this outside Eclipse then you have to right click on the "bin" folder and select "refresh" for Eclipse to see the new file.
您是将它复制到 Eclipse 内还是 Eclipse 外的 bin 文件夹中?如果您在 Eclipse 之外执行此操作,则必须右键单击“bin”文件夹并为 Eclipse 选择“刷新”以查看新文件。
A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.
bin 目录下相应文件夹(取决于其包)中的 *.class 文件应该可以做到。
回答by starblue
Project -> Properties -> Java Build Path -> Libraries -> Add External Class Folder
项目 -> 属性 -> Java 构建路径 -> 库 -> 添加外部类文件夹
The folder must contain a package hierarchy, i.e. if your class is really foo.bar.TokenizerImpl it must be in the subdirectory foo/bar.
该文件夹必须包含一个包层次结构,即如果您的类确实是 foo.bar.TokenizerImpl,它必须在子目录 foo/bar 中。
回答by Mario Ortegón
You could also JAR the class files that you want to add and add the JAR file to the build dependencies. To me this is the cleanest solution. Internally the JAR file has to have the correct directory structure, of course.
您还可以将要添加的类文件 JAR 并将 JAR 文件添加到构建依赖项中。对我来说,这是最干净的解决方案。当然,JAR 文件在内部必须具有正确的目录结构。
Right click into your project and select Java Build Path to add new dependencies.
右键单击您的项目并选择 Java Build Path 以添加新的依赖项。
Copying it into the bin folder won't work very well because it is meant to hold the result of compiled sources. As soon as you clean anything the file will be gone.
将其复制到 bin 文件夹中效果不佳,因为它旨在保存编译源的结果。一旦你清理了任何东西,文件就会消失。
回答by Tobias
or put everything into a jar file and add this as an external jar.
或将所有内容放入 jar 文件并将其添加为外部 jar。
回答by Shu Zhang
zip the class folder.
压缩类文件夹。
Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars", add the zipped file
突出显示项目名称,单击顶部工具栏中的“项目”,单击“属性”,单击“库”选项卡,单击“添加外部 jar”,添加压缩文件