27

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.

6 Answers 6

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.

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.

1
  • 2
    "Keep in mind, that you will have to specify the root folder containing the class files in their packages." +1 Sep 24, 2013 at 11:31
15

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

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.

0
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.

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.

0

or put everything into a jar file and add this as an external jar.

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.

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.

4
  • It is in the bin folder, but do I need to do anything to add it to the appropriate package? Just put it in the package folder, correct? Mar 19, 2009 at 5:06
  • If it is in the package com.stackoverflow.javacode then you would put the class file into the directory bin/com/stackoverflow/javacode/
    – Eddie
    Mar 19, 2009 at 5:09
  • BEWARE: If you ever clean the workspace, then your .class file will be gone! Mar 19, 2009 at 5:42
  • @dhiller: Yes, this is true.
    – Eddie
    Mar 19, 2009 at 14:29
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.