• Post Reply Bookmark Topic Watch Topic
  • New Topic

Importing a .class file into Eclipse, NoClassDefFoundError  RSS feed

 
Mark Jameson
Greenhorn
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't place a .class file in a package. A class already belongs to a package, regardless of where you put it.

Open a command prompt and run "javap Ostrich.class" to see the fully qualified name of the class.
 
Mark Jameson
Greenhorn
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to set up the PATH variable.
http://www.coderanch.com/how-to/java/how-to-create-java-program#SettingJavahomeAndPath

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.
 
Mark Jameson
Greenhorn
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I ran javap Ostrich.class and here is what was returned. I'm not seeing any package in the output.

 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This class is in a default package. You can't import it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hah gross, applets :P

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.
 
Mark Jameson
Greenhorn
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't import this because the .class file has a package of its own that you cannot change.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let this be a lesson. When you're writing your own classes, never put them in the default package. It almost always causes problems.
 
Mark Jameson
Greenhorn
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or (yuck) put your own classes in the default package. I guess that's what your teacher expects you to do.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use the unnamed (=“default”) package when you are starting, but by the time you have enough experience to use Eclipse you would be able to use package names.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!