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.
-
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.
Jergason : 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?Eddie : If it is in the package com.stackoverflow.javacode then you would put the class file into the directory bin/com/stackoverflow/javacode/dhiller : BEWARE: If you ever clean the workspace, then your .class file will be gone!Eddie : @dhiller: Yes, this is true. -
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, i.e. 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.
-
or put everything into a jar file and add this as an external jar.
0 comments:
Post a Comment