Apache lang3 works but Apache lang does not. Why?

2.3k views Asked by At

I have implemented the spring boot project with starter parent pom version 2.0.3. When I try to use

import org.apache.commons.lang.StringUtils

it throws error import cannot be resolved. Even though my m2 folder (C:\Users\.m2\repository\org\apache\commons) have lang package (commons-lang folder)installed. I also have commons-lang3 folder. If I change import to

import org.apache.commons.lang3.StringUtils

It works fine.

I have same version of starter pom in other spring boot project and there I am able to use commons.lang. Not able to identify the root cause.

2

There are 2 answers

4
Joachim Sauer On

As far as I can see the spring boot starter parent depends only on commons-lang3 and not on commons-lang (which is reasonable because commons-lang3 is a newer replacement for commons-lang).

It doesn't matter that your .m2 directory contains commons-lang (probably due to some other project depending on it): as long as your project hasn't declared a (direct or indirect) dependency on commons-lang, none of its classes will be on your classpath.

0
J Fabian Meier On

Call mvn dependency:tree on your "other" project. Here you can see through which path you drew commons-lang. Joachim Sauer is probably right and it is a transitive dependency that you started to use like a direct one.

Best fix would be to start to use commons-lang3 for your project (again, Joachim Sauer is right here), second best fix would be to declare commons-lang as a direct dependency in your pom.xml.