Reinstalling the maven dependency project raises a NoClassDefFoundError in an already running application

Let's say I have a very simple maven project ProjAthat itself has no dependencies. This project ProjAhas classes Xand Yas follows:

class X

package proja;

public class X {

    static {
        System.out.println("X loaded");
    }

    public void something() {
        System.out.println("X hello world");
    }

}

class Y

package proja;

public class Y {

    static {
        System.out.println("Y loaded");
    }

    public void something() {
        System.out.println("Y hello world");
    }

}

Proja.pom

<?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>com.tomac</groupId>
    <artifactId>ProjA</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

Then I have a second maven project ProjBthat has a project ProjAas a dependency.

I project ProjBI have a class Runas follows:

class run

package projb;

import proja.X;
import proja.Y;
import java.util.Scanner;

public class Run {

    public void run() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String msg = scanner.nextLine();
            switch (msg) {
                case "x":
                    new X().something();
                    break;
                case "y":
                    new Y().something();
                    break;
            }
        }
    }

    public static void main(String[] args) {
        new Run().run();
    }
}

Projb.pom

<?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>com.tomac</groupId>
    <artifactId>ProjB</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>ProjA</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

I install the project ProjAwith mvn installand then compile the project ProjBwithmvn compile

Now I run the main method from the class Runusing:
mvn exec:java -Dexec.mainClass="projb.Run"

Then I type X<ENTER> and get the output:

X loaded
X hello world

Y <ENTER> :

Y loaded
Y hello world

:

  • Run ( Run Scanner.nextLine())

  • X <ENTER> ( X X loaded X hello world)

  • , Run, - Y, , something(), : System.out.println("Y hello world new");

  • ProjA mvn install ( Y .m2)

  • Y <ENTER>

  • Y :

:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: proja/Y
    at projb.Run.run(Run.java:18)
    at projb.Run.main(Run.java:25)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: proja.Y
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 8 more

, , - , , ( , ) .

, main(). JVM.

. ?

: .

, (: something(String str)) , .

ProjB, - ProjA . , .

- (, , ) ProjA ProjB. , OutOfMemoryException.

- , .

/- > build- > run/restart, - - , ?

pom ProjA ProjB

+4
1

- , exec-maven-plugin Maven, Java.

Java- VM .

Maven, .m2, ( install ) SNAPSHOT ( , , ).

, dependency:build-classpath.

mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt -DincludeScope=runtime

classpath.txt , exec:java ( runtime, exec:java). classpath.txt jar, m2.

, Maven , classpath, Java .


, uber jar ( ) , , .

, / , .


SNAPSHOT versions:lock-snapshots:

pom -SNAPSHOT timestamp -SNAPSHOT, . -20090327.172306-4

, , / . / uber jar.

, , Maven, :

, . , , 1.0-SNAPSHOT, 1.0-20090128.202731-1. , . , , .

, , .

+1

Source: https://habr.com/ru/post/1656206/


All Articles