Jar can't load external classes

时间:2016-04-04 17:13:15

标签: java maven intellij-idea jar

I added external .jar into maven local repository by command mvn install:install-file -Dfile=simgrid.jar -DgroupId=LHCb -DartifactId=Sim2Sim -Dversion=3.12 -Dpackaging=jar -DgeneratePom=true. It successfully built. Then in pom.xml file I wrote using this answer

<?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>LHCb</groupId>
    <artifactId>Sim2Sim</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math3</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>LHCb</groupId>
            <artifactId>Sim2Sim</artifactId>
            <version>3.12</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>LHCb.Masterslave</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

So it successfully created .jar file. But when I run it by command java -jar nameOfJar.jar, error appeared. It says that it can't load classes from that external .jar file. How can I avoid it?

Exception in thread "main" java.lang.NoClassDefFoundError: org/simgrid/msg/NativeException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
    at java.lang.Class.getMethod0(Class.java:2866)
    at java.lang.Class.getMethod(Class.java:1676)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.simgrid.msg.NativeException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 6 more

1 个答案:

答案 0 :(得分:0)

您需要在命令行上指定运行时类路径-cp,并将其指向本地文件系统上的从属jar。鉴于您正在使用maven,新的可能位于〜/ .m2 / repository / LHCb / 3.12目录中,并且commons-math也可以在〜/ .m2 / repository / org / apache / commons /下找到commons-math3 / 3.2目录。您需要指定罐子,而不仅仅是目录。