简体   繁体   中英

How do I include .class files in my project in Eclipse? (Java)

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.

The file is TokenizerImpl.class, and I want to use it like this:

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?

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.

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.

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.

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.

Project -> Properties -> Java Build Path -> Libraries -> Add External Class Folder

The folder must contain a package hierarchy, ie if your class is really foo.bar.TokenizerImpl it must be in the subdirectory foo/bar.

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.

Right click into your project and select Java Build Path to add new dependencies.

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.

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

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.

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.

A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.

或者将所有内容放入jar文件中并将其添加为外部jar。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM