为何出现java.lang.ClassNotFoundException: org.apache.commons.text.WordUtils?
The issue here is straightforward—your run command is missing the commons-text jar from the classpath.
When you compiled with javac -d bin -cp lib/commons-text-1.2.jar MyClass.java, you told the compiler where to find the WordUtils class to validate your code. But when you run the program, the JVM needs access to that same jar file to actually load the WordUtils class at runtime. Without including it in the runtime classpath, the JVM can't locate the class, hence the ClassNotFoundException.
Corrected Run Command
On Linux/macOS:
java -cp bin:lib/commons-text-1.2.jar MyClass
On Windows:
java -cp bin;lib/commons-text-1.2.jar MyClass
(Note: Use : as the classpath separator on Unix-like systems, and ; on Windows.)
Bonus Tip
If you have multiple jars in the lib directory, you can use a wildcard to include all of them instead of listing each one:
Linux/macOS:
java -cp bin:lib/* MyClass
Windows:
java -cp bin;lib/* MyClass
内容的提问来源于stack exchange,提问作者spectrum

