简体   繁体   English

java.lang.ClassNotFoundException:Maven可运行jar的com.mysql.jdbc.Driver

[英]java.lang.ClassNotFoundException: com.mysql.jdbc.Driver for Maven runnable jar

I'm trying to create .jar file in my server(CentOS) using Maven. 我正在尝试使用Maven在服务器(CentOS)中创建.jar文件。 It is successfully compiling and generates the .jar file. 它正在成功编译并生成.jar文件。 But when I execute it, it shows the following error. 但是当我执行它时,它显示以下错误。 This questions may look like "previously asked" but, I tried all the suggestions from here. 这个问题可能看起来像“以前问过的”,但是,我尝试了这里的所有建议。 What I believe is in my POM.xml have some issue, but I'm not able to find. 我相信我的POM.xml中存在一些问题,但是找不到。 If someone can verify the POM.xml it would be helpful. 如果有人可以验证POM.xml,则将很有帮助。

Command: 命令:

$ java -jar target/ListDescCrawler-1.1-SNAPSHOT.jar

Error: 错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

My POM.xml 我的POM.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.uci.ics.crawler4j</groupId>
  <artifactId>ListDescCrawler</artifactId>
  <packaging>jar</packaging>
  <version>1.1-SNAPSHOT</version>
  <name>SanListDescCrawlerHandu</name>
  <url>http://maven.apache.org</url>
  <build>
      <pluginManagement>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>            
            </configuration>
          </plugin>
          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>edu.uci.ics.crawler4j.example.MainController</mainClass>                         
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
      </pluginManagement>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.james</groupId>
        <artifactId>apache-mime4j-core</artifactId>
        <version>0.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.james</groupId>
        <artifactId>apache-mime4j-dom</artifactId>
        <version>0.7</version>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>de.l3s.boilerpipe</groupId>
        <artifactId>boilerpipe</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-compress</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>fluent-hc</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-stax-api_1.0_spec</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.6</version>
    </dependency>   
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>com.sleepycat</groupId>
        <artifactId>je</artifactId>
        <version>4.0.92</version>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.8.1</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>com.drewnoakes</groupId>
        <artifactId>metadata-extractor</artifactId>
        <version>2.4.0-beta-1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
    <dependency>
        <groupId>org.ccil.cowan.tagsoup</groupId>
        <artifactId>tagsoup</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-app</artifactId>
        <version>1.6</version>
    </dependency>
  </dependencies>
</project>

The MANIFEST.MF file in the generated .jar file: 生成的.jar文件中的MANIFEST.MF文件:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: root
Build-Jdk: 1.7.0_91
Main-Class: edu.uci.ics.crawler4j.example.MainController
Class-Path: apache-mime4j-core-0.7.jar apache-mime4j-dom-0.7.jar asm-3
 .1.jar boilerpipe-1.1.0.jar commons-codec-1.6.jar commons-compress-1.
 3.jar commons-logging-1.1.3.jar fluent-hc-4.3.6.jar geronimo-stax-api
 _1.0_spec-1.0.1.jar httpclient-4.3.6.jar httpcore-4.3.3.jar httpmime-
 4.3.6.jar je-4.0.92.jar jsoup-1.8.1.jar log4j-1.2.17.jar metadata-ext
 ractor-2.4.0-beta-1.jar tagsoup-1.2.1.jar tika-app-1.6.jar tika-parse
 rs-1.6.jar tika-core-1.6.jar vorbis-java-tika-0.6.jar netcdf-4.2.20.j
 ar unidataCommon-4.2.20.jar jcip-annotations-1.0.jar commons-httpclie
 nt-3.1.jar jmatio-1.0.jar xz-1.5.jar pdfbox-1.8.6.jar fontbox-1.8.6.j
 ar jempbox-1.8.6.jar bcmail-jdk15-1.45.jar bcprov-jdk15-1.45.jar poi-
 3.11-beta2.jar poi-scratchpad-3.11-beta2.jar poi-ooxml-3.11-beta2.jar
  poi-ooxml-schemas-3.11-beta2.jar xmlbeans-2.6.0.jar asm-debug-all-4.
 1.jar isoparser-1.0.2.jar aspectjrt-1.8.0.jar rome-1.0.jar jdom-1.0.j
 ar vorbis-java-core-0.6.jar juniversalchardet-1.0.3.jar jhighlight-1.
 0.jar java-libpst-0.8.1.jar tika-serialization-1.6.jar tika-xmp-1.6.j
 ar xmpcore-5.1.2.jar slf4j-log4j12-1.5.6.jar slf4j-api-1.5.6.jar gson
 -1.7.1.jar

Commands executed: 执行的命令:

 mvn clean compile
 mvn clean package
 java -jar target/ListDescCrawler-1.1-SNAPSHOT.jar

There are many library files are added to the project including mysql-connector-java-5.1.34.jar. 有许多库文件添加到项目中,包括mysql-connector-java-5.1.34.jar。 But still the error is showing. 但是仍然显示错误。 I checked in SOF for a solution and tried many suggestions from here, but nothing fixed my issue. 我在SOF中找到了解决方案,并从此处尝试了许多建议,但没有任何解决办法解决我的问题。

I checked MANIFEST.MF in the generated .jar file and it looks fine. 我检查了生成的.jar文件中的MANIFEST.MF,它看起来不错。

Other points to note is, its working fine from Eclipse(without Maven) 需要注意的其他一点是,它在Eclipse(没有Maven)下可以正常工作

is there any mistake in my POM.xml? 我的POM.xml中有任何错误吗?

The suggestions I tried: 我尝试的建议:

  1. dependency/scope I tried all the possible values 依赖/范围我尝试了所有可能的值

  2. Copied mysql-connector-java-5.1.34.jar in the lib folder and changed the POM.xml accordingly 复制lib文件夹中的mysql-connector-java-5.1.34.jar并相应地更改POM.xml

and may other suggestions but nothing helped. 可能还有其他建议,但无济于事。

At last the things started to work with the following POM.xml 终于,下面的POM.xml开始起作用了

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>edu.uci.ics.crawler4j</groupId>
    <artifactId>ListDescCrawler</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SanListDescCrawlerHandu</name>
    <url>http://maven.apache.org</url>

    <properties>
        <jdk.version>1.7</jdk.version>      
        <junit.version>4.11</junit.version>
        <apache-mime4j-core.version>0.7</apache-mime4j-core.version>
        <apache-mime4j-dom.version>0.7</apache-mime4j-dom.version>
        <asm.version>3.1</asm.version>
        <boilerpipe.version>1.1.0</boilerpipe.version>
        <commons-codec.version>1.6</commons-codec.version>
        <commons-compress.version>1.3</commons-compress.version>
        <commons-logging.version>1.1.3</commons-logging.version>
        <fluent-hc.version>4.3.6</fluent-hc.version>
        <geronimo-stax-api_1.0_spec.version>1.0.1</geronimo-stax-api_1.0_spec.version>
        <httpclient.version>4.3.6</httpclient.version>
        <httpcore.version>4.3.3</httpcore.version>
        <httpmime.version>4.3.6</httpmime.version>
        <je.version>4.0.92</je.version>
        <jsoup.version>1.8.1</jsoup.version>
        <log4j.version>1.2.17</log4j.version>
        <metadata-extractor.version>2.4.0-beta-1</metadata-extractor.version>
        <mysql-connector-java.version>5.1.34</mysql-connector-java.version>
        <tagsoup.version>1.2.1</tagsoup.version>
        <tika-app.version>1.6</tika-app.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.james</groupId>
            <artifactId>apache-mime4j-core</artifactId>
            <version>${apache-mime4j-core.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.james</groupId>
            <artifactId>apache-mime4j-dom</artifactId>
            <version>${apache-mime4j-dom.version}</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>${asm.version}</version>
        </dependency>
        <dependency>
            <groupId>de.l3s.boilerpipe</groupId>
            <artifactId>boilerpipe</artifactId>
            <version>${boilerpipe.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>${commons-codec.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>${commons-compress.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>${commons-logging.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>fluent-hc</artifactId>
            <version>${fluent-hc.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-stax-api_1.0_spec</artifactId>
            <version>${geronimo-stax-api_1.0_spec.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient.version}</version>
        </dependency>   
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>${httpcore.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>${httpmime.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sleepycat</groupId>
            <artifactId>je</artifactId>
            <version>${je.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>${jsoup.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>com.drewnoakes</groupId>
            <artifactId>metadata-extractor</artifactId>
            <version>${metadata-extractor.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>
        <dependency>
            <groupId>org.ccil.cowan.tagsoup</groupId>
            <artifactId>tagsoup</artifactId>
            <version>${tagsoup.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-app</artifactId>
            <version>${tika-app.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>ListDescCrawler</finalName>
        <plugins>

            <!-- download source code in Eclipse, best practice -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>           

            <!-- Set a compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>

            <!-- Maven Assembly Plugin -->
            <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                  <manifest>
                    <mainClass>edu.uci.ics.crawler4j.example.MainController</mainClass>
                  </manifest>
                </archive>
              </configuration>
              <executions>
                <execution>
                  <phase>package</phase>
                  <goals>
                    <goal>single</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>

        </plugins>
    </build>

</project>

Eventhough your class path in MANIFEST.MF contains jar that you are dependent it does not mean that they actually on your class path when you run your project. 尽管MANIFEST.MF中的类路径包含您所依赖的jar,但这并不意味着它们在您运行项目时实际上就位于您的类路径上。 You have two option here either put all the dependent jar in the same directory of your runnable jar, or create a shaded jar. 您有两个选择,要么将所有从属jar放在可运行jar的同一目录中,要么创建一个有阴影的jar。

A shaded (fat) jar contains all of your dependencies. 一个有阴影的(胖)罐子包含您的所有依赖项。 You can create shaded jar using maven. 您可以使用Maven创建阴影罐。 Here is an example. 这是一个例子。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <finalName>${project.build.finalName}-with-dependencies</finalName>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass> edu.uci.ics.crawler4j.example.MainController </mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM