0

I used the following basic Maven command to generate a project:

mvn archetype:generate -DgroupId=it.maven.project -DartifactId=MavenExample -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

The project was correctly created and I could test the automatically generated App class without any problem with this instruction:

java -cp target/MavenExample-1.0-SNAPSHOT.jar:lib/* it.maven.project.App

Later on, I added some dependencies to the POM, obtaining the following file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>it.maven.project</groupId>
  <artifactId>MavenExample</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>MavenExample</name>
  <!-- FIXME change it to the project's website -->
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

     <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.10</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-collections4</artifactId>
      <version>4.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>



</project>

Finally, for testing sake, I modified the App class previously generated by Maven itself:

package it.maven.project;
import org.apache.commons.lang3.RandomStringUtils;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        System.out.println("Stringa generata casualmente: " + RandomStringUtils.random(16, true, true).toUpperCase());
    }
}

A series of strange things happen:

  1. even though more recent versions of dependencies are specified in POM, in .m2 folder the downloaded versions seem to be older (for instance I get 2.1 and 2.5 for commons-lang3)

  2. the project is correctly compiled by instruction

    mvn clean install -U

  3. when I run again command to execute App, I get the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/RandomStringUtils at it.maven.project.App.main(App.java:13) Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.RandomStringUtils at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more

Questions:

  • how is it possible that project compiles but then errors are returned at compiling time? Why the import in App is accepted but instruction:

    System.out.println("Stringa generata casualmente: " + RandomStringUtils.random(16, true, true).toUpperCase());

    generates an exception?

  • what should I correct/execute to allow my program to work and use correctly dependencies?

Chaffy
  • 182
  • 12
Bia
  • 165
  • 1
  • 3
  • 12

1 Answers1

0

when you create a simple java project you define a set of dependencies in the pom but if you don't create a java archive file (ejb-jar or war) all the dependencies are not available at runtime if you run the jar compiled in the target directory. There are two solutions:

  1. Create a uber jar that include all your depenencies in your jar: use stackoverflow solution

  2. when you run the jar from command line you have to add the dependencies to the classpath:

    java -cp "/path/dependencies/dep1.jar;/path/dependencies/dep2.jar" -jar myApp.jar

  • thanks for answer. is it possible to execute App without passing by a jar file, in such a way that dependencies are automatically found? – Bia Oct 12 '20 at 21:08