For class, I was given a .class file (no .java) and I need to create some JUnit tests of a method within that .class file. However, I’m not sure how to access the method. Incidentally, I’m using Eclipse.
I can import the file path, but it doesn’t see the .class file. Meaning . . . the file is /class/file/utility/StringUtility.class.
import class.file.utility.*; (works)
import class.file.utility.StringUtility; (gives error)
I tried using the first import, but I have a similar problem when I try to access the class file. Meaning . . .
StringUtility newUtil = new StringUtility(); (gives error – it doesn’t know what StringUtility is.
Any help would be appreciated.
SOLUTION BELOW SOLUTION BELOW SOLUTION BELOW
Since I am new to the forum I couldn’t answer my own question. However, here is the solution. . .
Thank you everyone for your help! You pointed me in the right direction and I was able to sort out my problem. In case someone else runs into the same issue, here is how I solved it. Again, I’m using Eclipse (Indigo).
1) File -> Import -> Archive File
Hit next
Then I browsed to the correct directory and selected the zip file with my .class file.
This added the archive to my project.
2) Right click on the project. Select Build Path -> Configure Build Path
Under the “Libraries” tab, I selected “Add Class Folder”. I then selected the folder I just added with the .class file.
I then entered StringUtil newUtil; and “Quick Fixed” the import to the code.
Hopefully, this will spare someone the four hours it took me to sort it out. 🙂
Again, thank you everyone for your comments. As I mentioned, they were helpful.
From a command line, you can run the
javapcommand to see what’s inside the class file and get your bearings. For example, if the file you received is calledTest.classtype this in the command prompt in the directory where the file is actually located:The above will allow you to find out the correct package for importing the class, and to see the method signatures in the class file.
Also, I suspect that the right import statement should be:
That is, assuming that your own classes are located in the correct package.