I have a misunderstanding of how to use maven provided
scope and the Java classloader.
Say I have a class, MyClass
, and this class has a serialize()
method that takes an enum that describes what JSON library to use when serializing.
One of these JSON libraries is internal, and included in the JAR that I distribute for MyClass. Simple.
The other JSON library is a 3rd party library that I want to force the user to provide. Thus, I put it under <scope>provided</scope>
in my pom.
Everything works for me during tests, but when I try to use my own library from the outside without the 3rd party JSON library in my classpath, I get ClassNotFoundException
s, even though I'm not making a call to serialize()
at all.
I was under the assumption that classes would only be loaded 'as-needed', and since I'm not making a call to serialize()
, I shouldn't have any runtime problems. Am I wrong? Is there no way to achieve what I am getting at here?
Found the issue. I had a static initializer that was making a call to the 3rd party JSON library which was causing the issue.
I do expect a class not found, if I'm a user of the api and not providing the 3rd party JAR. However, my concern was that I wasn't making any calls to cause the JAR to be needed. Static initializer threw me off.