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

泪湿孤枕 提交于 2021-02-07 20:42:09

问题


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?


回答1:


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 ;




回答2:


If you're using commons-text 1.4+, you have to use Java 8. Make sure you're using the right version of Java; That was the cause of my NoClassDefFoundError.



来源:https://stackoverflow.com/questions/49138525/java-lang-classnotfoundexception-org-apache-commons-text-wordutils

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!