Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: org.apache.commons.text.WordUtils

Tags:

java

apache

I can't figure out why I'm getting a ClassNotFoundException here, since I have compiled with the appropriate jar file for WorkUtils, and the class WordUtils is clearly in the jar file.

I compiled with javac -d bin -cp lib/commons-text-1.2.jar MyClass.java and run with java -cp bin MyClass

Here is my java class:

import org.apache.commons.text.WordUtils;

class MyClass
{
    public static void main(String[] args) {

        System.out.println(WordUtils.capitalize("sample text"));

    }
}

I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/text/WordUtils
    at MyClass.main(MyClass.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.text.WordUtils
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

Anyone know what could be going wrong here?

like image 468
spectrum Avatar asked Mar 06 '23 17:03

spectrum


1 Answers

The library must also be sent to java:

java -cp bin:lib/commons-text-1.2.jar MyClass

If you're on Windows, you may need to replace : with ;

like image 187
ernest_k Avatar answered Mar 12 '23 12:03

ernest_k