2

I created a maven project and add a dependency apache commons-io in pom.xml file.

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

I added dependency common-io so I can use commons.io.IOUtils in my project in order to read the file from the resource folder. /src/main/resources/file/file.txt.

Everything works during compiling and packaging but when I run the jar

java -cp target/maven-1.0-SNAPSHOT.jar com.sample.exercise.Main

I'm getting this error

       Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils
            at com.sample.exercise.Main.getFileWithUtil(Main.java:21)
            at com.sample.exercise.Main.main(Main.java:13)
        Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
            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)
            ... 2 more

Here's my code

    package com.sample.exercise;

        import java.io.*;
        import java.util.*;
        import org.apache.commons.io.IOUtils;

        public class Main{

          public static void main(String[] args) {
              Main main = new Main();
              System.out.println(main.getFile("file/fileTable.txt"));
        
          } 
    
          private String getFileString fileName) {
              String result = "";
              ClassLoader classLoader = getClass().getClassLoader();
              try {
                result =IOUtils.toString(classLoader.getResourceAsStream(fileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
           }
    }

What is the problem. can you help me with this? TIA

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
ilovejavaAJ
  • 1,631
  • 4
  • 26
  • 35

3 Answers3

0

Change the dependency to,

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

Try to do, maven clean, and maven install, maven commands are OS specific, however it does the same.

Try to add, below to your pom.xml

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
         <manifest>
            <mainClass>com.test.main</mainClass> // main being main.java which has my main method
         </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

<sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
peaceUser
  • 457
  • 5
  • 19
0

Or as given in the https://mvnrepository.com/artifact/commons-io/commons-io/2.4

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
  • execute `mvn dependency:tree` to see whether this commons-io jar is listed there. If not, I suspect the jar is installed in some other repository location. You may check this post to see how to check your local repository location http://stackoverflow.com/questions/38382889/include-local-jar-file-in-maven-project-not-finding-file/38398701#38398701 or alternatively you can execute your maven command from the command line itself instead of IDE because IDE uses embedded `Maven` and the repository location is different. – Ravindran Kanniah Jul 16 '16 at 06:10
  • I'm not using an IDE Im just using texteditor to code and terminal to run the code – ilovejavaAJ Jul 16 '16 at 06:40
0

I don't use IDE I'm just using texteditor (gedit) to code and terminal to run the program.

In that case, you are going to need to add the JAR files for all of your dependencies to the runtime classpath; e.g.

 $ java -classpath target/maven-1.0-SNAPSHOT.jar:path-to-commons-io-jar:... \
       com.sample.exercise.Main args

When you build / run in an IDE that understands Maven, it will add the dependencies to the runtime JAR for you automatically. When you run from the command prompt, you need to deal with that yourself ... or write a simple wrapper script that does this. (Or create an "uberJAR" that contains all of the dependencies ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216