3

I wrote a console application which reads a text file to String and then processes the file contents. I used maven in my project, enabled autoimport, added proper dependencies but still, when I try to process the String by using replace() method (this method belongs to org.apache.commons.lang3.StringUtils class) I get the undermentioned error. Moreover, when I launch my application in intelliJ, it works perfectly and everything seems to be fine. When I compile and build .jar file with maven and then launch it via terminal it reports this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils at com.company.Reader.process(Reader.java:47) at com.company.App.main(App.java:9) Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

Also, the dependencies in my pom.xml look like this:

 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.5</version>
    </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
          <version>3.4</version>
      </dependency>
      <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-library</artifactId>
      <version>1.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.8.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

I have no idea what might be wrong. I know a way to make my program work, that is, to download .jar with commons-lang3 manually and include it in my project, but this is not a satisfying solution for me. Does anyone know why do I get such error? Thanks in advance

Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
JacekDuszenko
  • 136
  • 2
  • 11

1 Answers1

2

You didn't put your entire pom.xml, but I guess that you forgot the dependency:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

You said that your project works inside Intellij, then you already have the common-lang3 in your computer.

I think the problem resides in the maven dependencies.

Try to execute: mvn clean install via command line in the root folder of your project.

You need to put your commons-lang3-3.4.jar in the classpath

Two links to help you:

https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

Run a JAR file from the command line and specify classpath

danilo
  • 7,680
  • 7
  • 43
  • 46
  • No, actually I did not forget this dependency. Now you can see my entire dependency section in pom.xml (before I just had missclicked and my paste wasn't added to this question, now I edited it) Also I run mvn clean install package everytime I change something in pom or in code while trying to fix this, so that is not the case. Though thanks for helping. – JacekDuszenko Nov 03 '17 at 01:07
  • How are you creating the jar? Can you show how you're creating and running the jar? You can use a plugin for maven for this: https://stackoverflow.com/questions/15869784/how-to-run-a-maven-created-jar-file-using-just-the-command-line – danilo Nov 03 '17 at 01:15
  • I am creating the jar with maven-jar-plugin from org.apache.maven.plugins. Also I've tried now to create the jar in your way and it fails - error message is : Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project task1: An exception occured while executing the Java class. – JacekDuszenko Nov 03 '17 at 01:30
  • I am making the jar by mvn clean install package. then i run it by moving to the directory in which jar is stored and using java -jar – JacekDuszenko Nov 03 '17 at 01:37
  • if you are running java -jar without specifying classpath, then commons-lang3-3.4.jar need to be on the same folder where you are running. – danilo Nov 03 '17 at 02:23
  • when you run `java -jar ` the jvm needs to know where are all .jars that the application will need. if they aren't in the same folder, you need to inform via -cp or via export classpath= / set classpath= – danilo Nov 03 '17 at 02:25
  • maven downloads jars inside .m2 folder, by default repository is located in ${user.home}/.m2 You need to copy the .jars inside a folder to run your program if you want to run without specifying classpath. – danilo Nov 03 '17 at 02:39
  • I thought that if i set parameter in pom.xml in my dependency to "provided" I will be able to run .jar without specifying the classpath. Am I wrong? – JacekDuszenko Nov 03 '17 at 02:58
  • It doesn't work even after placing commons-lang3.jar in the same directory and launching my .jar via java -cp commons-lang3.jar -jar myjarname.jar I don't know what might be wrong. Still the same exception. Do you have any ideas? – JacekDuszenko Nov 03 '17 at 03:04
  • The default classpath is the current directory. If you overrided the default, you have to include the current directory in the search path, you must to use "." https://stackoverflow.com/questions/26246436/is-the-current-path-in-the-classpath-by-default – danilo Nov 03 '17 at 03:15
  • "provided" is for another goal. It's for Application Servers, like Jboss that in some cases already has the jar. – danilo Nov 03 '17 at 03:20
  • to make it easier for you, maybe you could use a plugin to copy all the files in a directory: https://stackoverflow.com/questions/567996/can-maven-collect-all-the-dependent-jars-for-a-project-to-help-with-application – danilo Nov 03 '17 at 03:24
  • Okay, so I downloaded this plugin, copied all dependencies as jars to the target directory(it's where my application jar is located), I moved to this directory and I set the classpath to this directory CLASSPATH="./*" . Then I tried to java -jar but the error haven't disappeared. – JacekDuszenko Nov 03 '17 at 03:52
  • You need only to put "." and not ./* try this -cp . – danilo Nov 03 '17 at 04:08
  • classpath isn't relative, you can't specify ./ or ../ or something like this, you need specify absolute paths or . that represents the current directory – danilo Nov 03 '17 at 04:09
  • Okay, it worked. Immeasurably thank you for your patience and time sir. – JacekDuszenko Nov 03 '17 at 04:26