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);
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接口。卫生署。感谢您的帮助,我确实学到了很多关于日食的知识。
6 个解决方案
#1
40
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在Project属性中设置它。请记住,您必须在其包中指定包含类文件的根文件夹。
Therefore, if you wish to have the compiler access com.*.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/* as a class folder.
因此,如果您希望编译器访问项目A下的classes目录中的com.*.Example.class(但不在项目A的构建路径中),那么您应该将“classes”添加为类文件夹,并且不要将class / com / *作为类文件夹。
#2
14
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中。
#3
1
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文件夹将无法正常工作,因为它旨在保存已编译源的结果。一旦你清理任何东西,文件就会消失。
#4
0
or put everything into a jar file and add this as an external jar.
或者将所有内容放入jar文件中并将其添加为外部jar。
#5
0
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 two answers that 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选择“refresh”以查看新文件。
A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.
bin目录下相应文件夹(取决于其包)中的* .class文件应该这样做。
#6
0
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
突出显示项目名称,单击顶部工具栏中的“项目”,单击“属性”,单击“库”选项卡,单击“添加外部罐”,添加压缩文件
#1
40
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在Project属性中设置它。请记住,您必须在其包中指定包含类文件的根文件夹。
Therefore, if you wish to have the compiler access com.*.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/* as a class folder.
因此,如果您希望编译器访问项目A下的classes目录中的com.*.Example.class(但不在项目A的构建路径中),那么您应该将“classes”添加为类文件夹,并且不要将class / com / *作为类文件夹。
#2
14
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中。
#3
1
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文件夹将无法正常工作,因为它旨在保存已编译源的结果。一旦你清理任何东西,文件就会消失。
#4
0
or put everything into a jar file and add this as an external jar.
或者将所有内容放入jar文件中并将其添加为外部jar。
#5
0
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 two answers that 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选择“refresh”以查看新文件。
A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.
bin目录下相应文件夹(取决于其包)中的* .class文件应该这样做。
#6
0
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
突出显示项目名称,单击顶部工具栏中的“项目”,单击“属性”,单击“库”选项卡,单击“添加外部罐”,添加压缩文件