0

I'm getting a ClassNotFoundException when I try to execute the following code:

import org.apache.commons.text.WordUtils;

public class CapitalizeFirstLetters {

    public static void main(String[] args) {
        String str="this is a test";
        str=WordUtils.capitalizeFully(str);
        System.out.println(str);

    }

}

I'm new to importing libraries in Java. Here is what my Package Explorer and Build Path look like (I closely followed advice from other people asking similar questions).

Project Explorer , Java Build Path

and here is the error text:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
    at org.apache.commons.text.WordUtils.capitalizeFully(WordUtils.java:494)
    at org.apache.commons.text.WordUtils.capitalizeFully(WordUtils.java:464)
    at CapitalizeFirstLetters.main(CapitalizeFirstLetters.java:7)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 3 more

Can anyone please help me with this? I cannot find the answer for this problem in Eclipse anywhere online. So far I have tried: deleting everything and starting over.

Thank you!

Psycho
  • 1
  • 1
  • What is your error stack trace ? What is the `ClassNotFound` ? We will need more details to help. – Joachim Huet Nov 07 '17 at 16:21
  • @JoachimHuet I updated my original post – Psycho Nov 07 '17 at 16:24
  • If you do `import org.apache.commons.*;` instead of your import does it work better ? – Joachim Huet Nov 07 '17 at 16:26
  • @JoachimHuet now it says WordUtils cannot be resolved. It's like WordUtils is looking for itself in a different library or something – Psycho Nov 07 '17 at 16:31
  • Checkout this question: https://stackoverflow.com/questions/28504174/noclassdeffounderror-org-apache-commons-lang3-stringutils You need to also import commons-lang3. – Jens Nov 07 '17 at 16:32
  • That worked @Jens... I don't understand why it would require both an up-to-date library AND a deprecated library in order to run, but hey it works. Much appreciated! – Psycho Nov 07 '17 at 19:44
  • Good to hear. Apache commons-text and Apache commons-lang are two different libs. And the class `WordUtils` from commons-text uses the class `StringUtils` from commons-lang. That is why you need both libs. (And that is why people use Maven because Maven will automatically load all transitive dependencies. Transitive means all further dependencies. You just need to say which libs you want and if those have further dependencies maven will load them all and put them on your classpath.) ;-) – Jens Nov 09 '17 at 10:44

0 Answers0