Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils [duplicate]

I am testing out maven and its capabilities. I am trying to write a string to a text file using commons-io

import org.apache.commons.io.FileUtils;
...
public void writeToFile(String fileName){
    File file = new File(fileName);
    ...
    FileUtils.writeStringToFile(file,rowEntry); //rowEntry is a String
}  

I have added the commons-io to the dependencies

POM.xml

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
 </dependency>  

It compiles but it throws an exception when I run it

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
at com.RandCollections.StringFinder.writeToFile(StringFinder.java:704)
at com.RandCollections.StringFinder.menu(StringFinder.java:123)
at com.RandCollections.StringFinderMain.main(StringFinderMain.java:28)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more   

I think Im missing out on some things, can you help point it out please?

like image 380
Dee Avatar asked Jun 22 '16 06:06

Dee


1 Answers

If it compiles fine but throws the exception when running, it means the dependency was on classpath when compiling but not when running the code.

Maven is responsible for compile classpath, and did provide the dependency on compile time. You'll have to check how you run the application and make sure the maven dependencies are also on the runtime classpath - that has nothing to do with Maven, unless you run the code as a part of unit tests.

like image 92
Jiri Tousek Avatar answered Oct 24 '22 00:10

Jiri Tousek