如何修复Maven中的No Manifest Attribute错误?

时间:2018-08-07 16:24:23

标签: java maven jar

当我尝试运行具有外部依赖关系而不是外部依赖关系的jar文件时,我尝试使用下面的代码修复"Error: No manifest attribute"

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        ...
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>Driver</mainClass>
            </manifest>
          </archive>
        </configuration>
        ...
      </plugin>
    </plugins>

但是,每次我清洗,编译,打包并运行jar时,都将执行。它一直在说:

Error: Could not find or load main class com.harriott.Driver
Caused by: java.lang.ClassNotFoundException: Driver

P.S。我只有一个带有Public static void main方法的类,它名为Driver:

<?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.harriott</groupId>
    <artifactId>UpsDriver</artifactId>
    <version>1.0</version>





    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
            </plugin>

            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>Driver</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>




        </plugins>
    </build>

    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>






   <dependencies>


        <dependency>
            <groupId>com.cloakware.cspm.client</groupId>
            <artifactId>CommonLogging</artifactId>
            <version>1.00</version>

        </dependency>

       <dependency>
           <groupId>com.cloakware.cspm.client</groupId>
           <artifactId>CommonsNet</artifactId>
           <version>1.00</version>

       </dependency>

       <dependency>
           <groupId>com.cloakware.cspm.client</groupId>
           <artifactId>cwjcafips</artifactId>
           <version>1.00</version>

       </dependency>

       <dependency>
           <groupId>com.cloakware.cspm.client</groupId>
           <artifactId>cwjssefips</artifactId>
           <version>1.00</version>

       </dependency>

       <dependency>
           <groupId>com.cloakware.cspm.client</groupId>
           <artifactId>MortbayJetty</artifactId>
           <version>1.00</version>

       </dependency>

       <dependency>
           <groupId>com.cloakware.cspm.client</groupId>
           <artifactId>JavaxServlet</artifactId>
           <version>1.00</version>

       </dependency>

       <dependency>
           <groupId>com.cloakware.cspm.client</groupId>
           <artifactId>CSPMClient</artifactId>
           <version>1.00</version>

       </dependency>

    </dependencies>


</project>

这是我的Java代码:

import com.cloakware.cspm.client.CSPMClient;

public class Driver {
    /**
     * Main entry point.
     *
     * @param "args[0]", String target alias
     *
     * @param "args[1]", bypass cache flag. If set to:
     *
     * "true", the cspm client will call the cspm server system
     *
     * "false", the cspm client will 1st search the local cache
     *@param "args[2]", xmlOption. (Optional) If set to:
     *
     * "-x", Gives the XML data.
     * @return int 0 if successful, 100 if an exception ocurred, otherwise
     * documented error codes for the CSPMClient class.
     *
     */
    public static void main(String[] args) {





        try {

            CSPMClient testInterface = new CSPMClient();

            System.out.println("------------------------------------");
            System.out.println(testInterface.getAllCredentials());
            //System.out.println(testInterface.retrieveCredentials("emc_grab",true));
            System.out.println("------------------------------------");

            //get the result
            String statusCode = testInterface.getStatusCode();
            String userId = testInterface.getUserId();
            String password = testInterface.getPassword();
            String xmlData = testInterface.getXMLData();
            System.out.println("Status Code: " + statusCode);
            System.out.println("UsedId: " + userId);
            System.out.println("Password: " + password);
            System.out.println("XML Data: " + xmlData);


            //set the return value
            if (statusCode.equals("400")) {
                System.out.println("PASSED");
                System.exit(0);
            } else {
                System.out.println("FAILED");
                System.exit(Integer.parseInt(statusCode));
            }

        } catch(Exception e) {
            e.printStackTrace();
            System.exit(100);
        }catch(UnsatisfiedLinkError unsatisfiedLinkError){
            System.out.println("UnsatisfiedLinkError>>>>");
            System.out.println(unsatisfiedLinkError.getMessage());
        }

    }
}

我也会收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/cloakware/cspm/client/CSPMClient
        at Driver.main(Driver.java:29)
Caused by: java.lang.ClassNotFoundException: com.cloakware.cspm.client.CSPMClient
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)

0 个答案:

没有答案