I have a .class file which I need to import into Eclipse, Using Eclipse, I imported the .class file into a Java project, placed it into a package named and then exported the package as a .jar file -- I then add to the build path of my Main class in Properties>Java Build Path>Libraries>Add External JARs. I can then import the class into my program. However, when I try to enter , Java throws this exception:
I have followed all the instructions I can find on importing a .class file and none of them seem to work due to this problem. When I import the file using Properties>Java Build Path>Libraries>Add External Class Folder... while the file is in a folder structure ostrich\Ostrich\Ostrich.class I get the same problem.
When I open cmd.exe and try to use javap, Windows tells me that javap is not a recognized command. In addition, I had to place the .class file into a package because Java does not let you import from the default package. Or am I missing something?
Java doesn't let you import classes from default package. That is true.
You can't change the package of a compiled class.
Use javap and check what is the fully qualified name of the class as Stephan suggested.
Also, I (and many other Ranchers) believe that the best way to learn Java is by writing code in a good text editor (for example Notepad++) and compiling/running the program using command line.
You need to know how to do this to fully understand how Java works. IDEs hide this from you.
Okay, you can use this class in two ways. Either you use the class directly by putting it on the class path in your project (I'm not sure how to do this in Eclipse, I use NetBeans), or you put it in a jar, and put that jar on your project's class path. It seems you already tried the latter option.
Use the command prompt to run "jar -cf Ostrich.jar Ostrich.class" to archive the class file. You can now add Ostrich.jar to your project. Ostrich is in the default package, so any classes you write that want to use it, will have to be in the default package as well.
Okay, so when I place the Ostrich.class file in my Eclipse Workspace such that the file structure is <Eclipse Workspace>\Ostrich\Ostrich<1>\Ostrich.class and then export the folder Ostrich<1> as a JAR file containing only Ostrich.class such that the JAR file file will contain the file Ostrich.class in the structure Ostrich.jar\Ostrich\Ostrich.class, I can then import this JAR file into my other Eclipse project. In my other project, I add the JAR file as an External JAR to the Build Path in the Libraries heading. When I type
Eclipse seems to import it fine. My research reveals that the NoClassDefFoundError is caused when a Class is present when compiling but not when running, so I am led to believe that the ostrich.jar file is not being found when I attempt to run the file. Is this correct, or is it true that I can't import this because the .class file has a package of its own that I cannot change?
I didn't write this class. My teacher for AP Computer Science wrote it and he wanted us to use the class in our work. He also has us using an IDE - DrJava. I have already taken the class and am trying to extend it to be more useful. I could probably convince him to send me the source so I can edit it directly or at least assign a package to the .class file but for now I do not have access to the source in any way. When the other students try to use DrJava they can import the .class file just fine by having it in the same directory as their .java files, so there is something I am missing here.
You're not missing anything. I doubt that they can use the class from their own classes that are not in the default package.
If you can't get the source from your teacher, or if you can't convince him to change the package, you can solve this problem in a difficult, roundabout way using Ostrich factories and reflection. You really don't want to go there.
There's another way to do this, but it's only for the sake of discussion, and shouldn't actually be attempted:
You can make an ostrich.Ostrich interface that has the same signatures as the ones you see when you run "javap Ostrich.class". In the default package, you create an OstrichFactory class that implements ostrich.OstrichFactory which returns anonymous instances of ostrich.Ostrich that are wrapped around an instance of Ostrich. Now you can load OstrichFactory through reflection, and let it create instances of ostrich.Ostrich which simply delegate to Ostrich.
Conceptually it would work, but it's not worth the trouble.