0

Just a quick question regarding Java's reflection mechanism. My program should load the .class items from a .jar file.

I get a java.lang.NoClassDefFoundError for classes within the jar file, that are importing outer packages (to which I don't have access). Is there any chance of passing the exception without having to import the required packages.

Thanks.

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • 1
    You have classes in Jar *A*, that depend on classes in another Jar *B*, but you're only including Jar *A* in your classpath, is that what you're saying? No, there is no way to load these classes, since their implementation depends on missing code. What are you actually trying to do? – dimo414 Oct 19 '15 at 16:39
  • yes, that's pretty much it :). TY – KarateCoder Oct 19 '15 at 16:41
  • What do you mean with "don't have access"? You do not know how to add it to your runtime classpath or the jar file just is not there? – CoronA Oct 20 '15 at 05:45
  • don't have the Jar file available. There is a dependency problem. – KarateCoder Oct 22 '15 at 11:29

2 Answers2

0

I don't believe so - in order for reflection to load, you have to load the class using a class loader and then can, for example, instantiate objects and execute methods, so you will need the dependencies.

If you don't want to instantiate or execute, then a bytecode library would allow you to inspect the contents of the class file.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
0

Others are correct, you cannot import code from dependencies of dependencies, directly.

That being said, a great way to prevent this problem is to use build technologies like Maven and Gradle. When you import dependencies with either of those techs, you automatically import dependencies of dependencies.

entpnerd
  • 10,049
  • 8
  • 47
  • 68