线程中的maven异常" main" java.lang.NoClassDefFoundError:

时间:2015-01-21 10:13:37

标签: maven

我喜欢使用maven

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>chinahighway.InitHighWay</mainClass>
                            -->入口类名
                        </manifest> 
                    </archive>
                </configuration>
            </plugin>
一切正常。 但我想在类路径中添加一些本地jar,所以我修改如

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>chinahighway.InitHighWay</mainClass>
                            -->入口类名
                        </manifest>
                        <manifestEntries>
                            <Class-Path>D:\chinahighway\beyondbjdbc\iijdbc</Class-Path>
                            <Class-Path>D:\chinahighway\beyondbjdbc\hibernate-beyondb</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
mvn clean install -DskipTests

之后

java -jar ....

  

线程“main”中的异常java.lang.NoClassDefFoundError:   chinahighway / InitHighWay

如何解决它并可以将jar添加到类路径? 请帮助我,非常感谢。

3 个答案:

答案 0 :(得分:0)

尝试添加

<dependency>
    <groupId>com.ingres.jdbc</groupId>
    <artifactId>iijdbc</artifactId>
    <version>9.2-3.4.10</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/iijdbc.jar</systemPath>
</dependency>

答案 1 :(得分:0)

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>chinahighway.InitHighWay</mainClass>
                            -->入口类名
                        </manifest>
                         <manifestEntries>
                            <Class-Path>lib/iijdbc.jar</Class-Path>
                            <Class-Path>lib/hibernate-beyondb.jar</Class-Path>
                        </manifestEntries> 
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

enter image description here

答案 2 :(得分:0)

如果jar不是groupId-artifactId-version.jar 你应该添加

<dependency>
        <groupId>beyondb</groupId>
        <artifactId>iijdbc</artifactId>
        <version>1</version>
        <scope>system</scope>
        <systemPath>D:\chinahighway\beyondbjdbc\iijdbc.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>beyondb</groupId>
        <artifactId>hibernate</artifactId>
        <version>1</version>
        <scope>system</scope>
        <systemPath>D:\chinahighway\beyondbjdbc\hibernate-beyondb.jar</systemPath>
    </dependency>

这将生成hibernate-1.jariijdbc-1.jar, 并创建以下目录

created directories

并删除添加的内容并添加

    <repositories>
            <!--other repositories if any -->
            <repository>
                <id>project.local</id>
                <name>project</name>
                <url>file:D:\wyr\chinahighway\highway\src\main\resources\rep</url>
            </repository>
        </repositories>
<dependencies>
            <dependency>
                <groupId>beyondb</groupId>
                <artifactId>iijdbc</artifactId>
                <version>1</version>
            </dependency>
            <dependency>
                <groupId>beyondb</groupId>
                <artifactId>hibernate</artifactId>
                <version>1</version>
            </dependency>
<dependencies>

这将解决您的问题 Reference