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

时间:2015-11-04 12:45:18

标签: java mysql maven jdbc

我正在尝试使用Maven在我的服务器(CentOS)中创建.jar文件。它成功编译并生成.jar文件。但是当我执行它时,它显示以下错误。这个问题可能看起来像“之前被问到”但是,我尝试了这里的所有建议。我认为在我的POM.xml中有一些问题,但我无法找到。如果有人可以验证POM.xml,那将会有所帮助。

命令:

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

错误:

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

我的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>

生成的.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

执行的命令:

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

项目中添加了许多库文件,包括mysql-connector-java-5.1.34.jar。但仍然出现错误。我检查了SOF的解决方案,并从这里尝试了很多建议,但没有解决我的问题。

我在生成的.jar文件中检查了MANIFEST.MF,看起来很好。

需要注意的其他要点是,Eclipse的工作正常(没有Maven)

我的POM.xml中有错误吗?

我尝试的建议:

  1. 依赖/范围我尝试了所有可能的值

  2. 在lib文件夹中复制mysql-connector-java-5.1.34.jar并相应地更改了POM.xml

  3. 可能还有其他建议,但没有任何帮助。

    最后,事情开始使用以下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>
    

1 个答案:

答案 0 :(得分:2)

尽管MANIFEST.MF中的类路径包含您依赖的jar,但这并不意味着它们在您运行项目时实际上在您的类路径上。这里有两个选项,要么将所有依赖的jar放在runnable jar的同一目录中,要么创建一个带阴影的jar。

阴影(胖)jar包含所有依赖项。您可以使用maven创建阴影jar。这是一个例子。

<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>