Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 903
jergason Avatar asked Mar 19 '09 04:03

jergason


People also ask

How do I add a class to a project in Java?

In the Project window, right-click a Java file or folder, and select New > Java Class. Alternatively, select a Java file or folder in the Project window, or click in a Java file in the Code Editor. Then select File > New > Java Class. The item you select determines the default package for the new class or type.

How do I get .class in Eclipse?

In Eclipse IDE, you can type CTRL + SHIFT + T in Windows or *nix or Command + SHIFT + T in Mac OSX to prompt an Open Type dialog box to find details about a specified Java class. For example, if you want to know the detail of this Java class – FlatFileItemWriter (Spring batch class).

Where do I put .class files?

The default classpath is in the current directory. Usually you put Java classes in packages in a hierarchical directory structure in the filesystem, in which case the Java compiler will also put . class files in a corresponding structure.


1 Answers

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.

like image 166
Vineet Reynolds Avatar answered Oct 23 '22 12:10

Vineet Reynolds