运行maven jar文件时出错

时间:2017-02-05 02:24:20

标签: java maven hadoop hdfs bigdata

我从eclipse创建了maven jar文件。运行jar时出现以下错误。

错误:

 A JNI error has occurred, please check your installation and try again

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FSDataOutputStream
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source) 
    at java.lang.Class.privateGetMethodRecursive(Unknown Source)

请指导是否需要在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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>utd.bigdata</groupId>
  <artifactId>hadoop1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>hw1</name>
  <dependencies>
  <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.4.1</version>
  </dependency>
   <dependency>
     <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.7.3</version>
        <scope>compile</scope>
       </dependency>
  <!--  <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-auth</artifactId>
    <version>2.4.1</version>
</dependency>-->
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>bigdata.UploadHadoop</mainClass>
                        <classpathPrefix>classes/lib</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

pom.xml中是否有任何错误?请建议任何代码更改

3 个答案:

答案 0 :(得分:1)

常规的.jar存档在运行时不包含其依赖项,为此您可以使用maven-assembly-plugin创建一个胖的可执行文件Jar:

enter image description here

<!-- Create a fat Jar -->
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <archive>
      <manifest>
        <mainClass>bigdata.UploadHadoop</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>

答案 1 :(得分:0)

作为&#34;胖罐&#34;的替代品,hadoop jar跑步者也支持&#34; jar罐子&#34;格式。值得注意的是,它不是jar的标准,但是因为它在hadoop中得到支持,所以它是:How do I put all required JAR files in a library folder inside the final JAR file with Maven?

这种格式是普通的jar,但是包含所有依赖关系jar的lib /文件夹。

答案 2 :(得分:0)

如果您使用maven-jar-plugin构建可执行jar,请阅读该文档。 http://maven.apache.org/shared/maven-archiver/examples/classpath.html#Make

首先,在pom.xml中添加maven-jar-plugin

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        ...
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>bigdata.UploadHadoop</mainClass>
              <classpathPrefix>classes/lib</classpathPrefix>
            </manifest>
          </archive>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>

其次,它没有将依赖关系jar附加到项目jar。因此,您必须确保依赖jars(hadoop-client,hadoop-common)自己在classes / lib中。

最后,我建议您使用maven-shade-plugin构建一个可执行jar(胖罐) http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html 它会将依赖jar附加到结果shade jar中。 您还使用maven-assembly-plugin构建可执行jar,但不建议使用。

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>bigdata.UploadHadoop</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>