Exception in thread “main“ java.lang.NoClassDefFoundError無依賴問題与Maven 项目生成jar运行时提示“没有主清单属性”

Exception in thread "main" java.lang.NoClassDefFoundError無依賴問題

  Exception in thread "main" java.lang.NoClassDefFoundError     

         java -jar original-webservice-1.0-SNAPSHOT.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/axis/client/Call
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:681)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:663)
Caused by: java.lang.ClassNotFoundException: org.apache.axis.client.Call
    at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 7 more
 

        出現這個問題

         查看原因是應為jar沒有這個call依賴

        我使用的maven打的包

        maven沒有主動把包導進去

        網上查了很多,都是springboot的項目

        像我們自己寫的非springboot,非web,C/S,B/S的項目

·       像這種都是使用spring-boot-maven-plugin來構建的,並不適用

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <classifier>exec</classifier>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

        找了幾個非springboot的也沒有講解

        自己理解了一下

        大概是缺少3個 org.apache.maven.plugins 的插件

        

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</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>
                            <!-- 此处为程序主入口-->
                            <mainClass>com.test.test</mainClass>
                        </manifest>
                    </archive>
                </configuration>

            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.test.test</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

        三個plugin裡面

        

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

        需要改的是紅色部分,version是版本,source,target是java版本


     這個是導成jar包的plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <!-- 此处为程序主入口-->
                <mainClass>com.test.test</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

        紅色部分要改成你自己的

         有水印不知道你們看不看得清

         這個包名+帶有main函數的類(也稱主類)

        如果你沒有包名

        

         比如直接在java下建立test主類,那麼就應該改成<mainClass>com.test.test</mainClass>這樣就行了


這個才是主要把程序和依賴放在一起的plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.test.test</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

         同理第三個包也是改這個地方

        

         讓後打包

        會生成

         兩個jar包,一個原始包,一個完整包

        我們用第二個有完整依賴的包

         java -jar webservice-1.0-SNAPSHOT.jar

        這三個如果爆紅,那麼就要放入dependencies里下載

        

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>axis</artifactId>
    <version>3.1</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
</dependency>

Maven 项目生成jar运行时提示“没有主清单属性”

        這個問題是沒有指定主程序入口

        就是這一段

        這個是導成jar包的plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <!-- 此处为程序主入口-->
                <mainClass>com.test.test</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

        紅色部分要改成你自己的

         有水印不知道你們看不看得清

         這個包名+帶有main函數的類(也稱主類)

        如果你沒有包名

        

         比如直接在java下建立test主類,那麼就應該改成<mainClass>com.test.test</mainClass>這樣就行了

        就能解決

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值