Search code examples
javamavenmaven-dependency

Maven not downloading dependencies


I'm trying to get Maven to download dependencies. I'm using the commands I believe you are supposed to use, and this is just pure command line, no IDE involvement, but it still isn't happening.

The problem is also with my own project, but I'll use this one by someone else as an example: https://github.com/stephanrauh/BeyondJava.net-Articles/tree/master/MethodModificationWithASM

I've tried the following commands:

mvn compile
mvn test
mvn package
mvn install
mvn dependency:resolve

and all appear to run correctly, even going so far as to claim to have successfully downloaded dependencies:

[INFO]
[INFO] The following files have been resolved:
[INFO]    javassist:javassist:jar:3.12.1.GA:compile
[INFO]    org.ow2.asm:asm:jar:5.0.3:compile
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.690 s
[INFO] Finished at: 2020-01-13T16:13:34Z
[INFO] ------------------------------------------------------------------------

but when I try to actually run the compiled program, I get an error:

C:\BeyondJava.net-Articles\MethodModificationWithASM>java -jar target\MethodModificationWithASM-0.0.1-SNAPSHOT.jar
Error: Unable to initialize main class de.beyondjava.demos.bytecode.Main
Caused by: java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor

and the same when I try to run from the .class files instead of the jar. And the error is correct: the asm jar is in fact missing:

C:\BeyondJava.net-Articles\MethodModificationWithASM>dir /s *.jar
 Volume in drive C is Windows
 Volume Serial Number is 04EE-7EB0

 Directory of C:\BeyondJava.net-Articles\MethodModificationWithASM\target

13/01/2020  16:01             8,144 MethodModificationWithASM-0.0.1-SNAPSHOT.jar
               1 File(s)          8,144 bytes

     Total Files Listed:
               1 File(s)          8,144 bytes
               0 Dir(s)  164,672,442,368 bytes free

nor has it been squirreled away anywhere else; I searched the entire hard disk.

The pom.xml seems to specify the dependency okay, and Maven seems to be happy that it has done so:

<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>de.beyondjava.demos.bytecode</groupId>
  <artifactId>MethodModificationWithASM</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MethodModificationWithASM</name>
  <description>This demo shows how to create and run byte code in a Java program.
</description>
  <dependencies>
    <dependency>
      <groupId>org.ow2.asm</groupId>
      <artifactId>asm</artifactId>
      <version>5.0.3</version>
    </dependency>
    <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.12.1.GA</version>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

So what am I missing?


Solution

  • You cannot just run a jar on the command line without a proper classpath. Maven downloads the artifacts to .m2/repository in your home directory and uses them for the build, but it does not copy them to target or add them to the produced jar.

    Most jars are used as libraries and for them, the behaviour is just fine. If you want to run a jar from the command line, it is better to create a jar with dependencies:

    How can I create an executable JAR with dependencies using Maven?