• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Maven NoClassDefError When Running Jar - Works Fine In Eclipse

 
Ranch Hand
Posts: 193
14
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I've created a Maven Project in Eclipse to copy files from one location to another using the org.apache.commons.io.FileUtils class from Apache Commons IO. The code runs fine and successfully copies files when run in Eclipse and when I run the project as a Maven package configuration it successfully downloads the dependencies I've specified in my pom.xml file to the target directory as well as creating the JAR file. When I try and run the JAR from the command prompt I get the following error:


Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
at com.statestreet.common.App.main(App.java:26)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more


This is despite the fact that I've specified Apache Commons IO as a dependency. Can someone please assist? Here is my code and pom for reference






 
Ranch Hand
Posts: 376
2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us your command line ?
all seem that you are not adding commons.io neither commons.lang jar files
 
Simon Ritchie
Ranch Hand
Posts: 193
14
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried the following approaches

In Eclipse -> Run -> Run Configurations -> Maven Build:
  • install:install-file -Dfile=C:\Java\commons-io-2.4\commons-io-2.4.jar -DgroupId=org.apache -DartifactId=commons-io -Dversion=2.4 -Dpackaging=jar
  • install:install-file -Dfile=C:\Java\commons-lang3-3.4\commons-lang3-3.4.jar -DgroupId=org.apache -DartifactId=commons-lang -Dversion=2.6 -Dpackaging=jar
  • compile (Success but with Warnings)
  • package (Success but with Warnings)
  • install (Success but with Warnings)


  • The app runs fine and the JAR is generated.

    In Command Prompt ->
  • Navigate to directory with pom.xml
  • mvn compile (Success but with Warnings)
  • mvn package (Success but with Warnings)
  • mvn install (Success but with Warnings)
  • Navigate to target directory
  • java -jar Backup-0.0.1-SNAPSHOT.jar "<Source path of directory I want copied>" "<Destination path of directory I want copied>"



  • This is the command line output, if that's what you're asking?



     
    German Gonzalez-Morris
    Ranch Hand
    Posts: 376
    2
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok. your command line is:



    you need to add :

    C:\Java\commons-lang3-3.4\commons-lang3-3.4.jar
    C:\Java\commons-io-2.4\commons-io-2.4.jar



    (assuming that's the correct path)

    then it will become:

     
    Simon Ritchie
    Ranch Hand
    Posts: 193
    14
    Hibernate Eclipse IDE Spring
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Those are the correct paths but I'm afraid the error is the same

     
    German Gonzalez-Morris
    Ranch Hand
    Posts: 376
    2
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    and if you use slash instead backslash ?

     
    Simon Ritchie
    Ranch Hand
    Posts: 193
    14
    Hibernate Eclipse IDE Spring
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm beginning to think it is not a Maven issue. I have just tried to javac the main class in the command prompt and I am getting the error



    This is strange. No such error appears in Eclipse (the code compiles and runs fine). I know that FileUtils is dependent on Java 7 to run. My installed JRE is 1.8.0_66 and my installed JDK is 1.7.0_80. Something's not right here.

    Thanks for the help German but I think I may have been barking up the wrong tree by placing this question on the Build Tools forum...!
     
    German Gonzalez-Morris
    Ranch Hand
    Posts: 376
    2
    Eclipse IDE Firefox Browser Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sure, it is a classpath issue. In Eclipse works because you have the correct jar file in the classpath.
     
    Simon Ritchie
    Ranch Hand
    Posts: 193
    14
    Hibernate Eclipse IDE Spring
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How do I resolve this issue if I want to distribute a JAR file? I thought that by using Maven any dependencies I specified would be included in the classes distributed with the code?
     
    German Gonzalez-Morris
    Ranch Hand
    Posts: 376
    2
    Eclipse IDE Firefox Browser Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    you can keep working with Maven.

    add the following plugin to pom.xml:



    and run it with:


     
    Simon Ritchie
    Ranch Hand
    Posts: 193
    14
    Hibernate Eclipse IDE Spring
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks German
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic