通过maven创建jar的问题

发布于 2024-10-18 02:10:28 字数 6433 浏览 1 评论 0原文

你好,

我正在尝试使用 maven 创建可执行 jar。 jar 已创建,当我尝试执行 java -jar myjar 时,出现以下错误

引起:java.lang.ClassNotFoundException:org.apache.hadoop.conf.Configuration 在 java.net.URLClassLoader$1.run(URLClassLoader.java:202)

我提取了 jar 并看到 hadoop 类不存在。在日志中我可以看到 hadoop jar 没有被复制。问题出在哪里

我的 pom.xml 如下:

<构建>

<插件>

<插件>

org.apache.maven.plugins
maven-compiler-plugin
<版本>2.3.1
<配置>
<来源>1.6
<目标>1.6


<插件>
org.apache.maven.plugins
maven- assembly-plugin
<版本>2.2-beta-4
<配置>
<描述符引用>
jar-with-dependencies

<存档>
<清单>
com.MyClass



<处决> <执行>
<阶段>封装
<目标>
<目标>单个





    <!-- Compile time dependencies -->
<dependency>
  <groupId>bixo</groupId>
 <artifactId>bixo-core</artifactId>
     <version>1.0-SNAPSHOT</version>
</dependency>   
    <dependency>
        <groupId>cascading</groupId>
        <artifactId>cascading-core</artifactId>
    </dependency>

    <dependency>
        <groupId>cascading</groupId>
        <artifactId>cascading-11-jdbc</artifactId>
        <version>0.0.4</version>
    </dependency>

    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.7</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.0.1</version>
    </dependency>

 <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-parsers</artifactId>
        <version>0.8</version>
    </dependency>

    <dependency>
        <groupId>org.archive</groupId>
        <artifactId>heritrix</artifactId>
        <version>1.14.3</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.4</version>
    </dependency>

    <dependency>
        <groupId>org.kohsuke</groupId>
        <artifactId>args4j</artifactId>
        <version>2.0.10</version>
    </dependency>


    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-core</artifactId>
        <version>0.21.0-SNAPSHOT</version>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <artifactId>jetty</artifactId>
                <groupId>org.mortbay.jetty</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jetty-util</artifactId>
                <groupId>org.mortbay.jetty</groupId>
            </exclusion>
        </exclusions>
    </dependency>
  <dependency> 
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapred</artifactId>
            <version>0.21.0-SNAPSHOT</version>
            <scope>provided</scope>
</dependency>
 <dependency> 
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>0.21.0-SNAPSHOT</version>
            <scope>provided</scope>
</dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <!-- Eclipse project generation dependencies -->

    <dependency>
        <groupId>ant-eclipse</groupId>
        <artifactId>ant-eclipse-jvm1.2</artifactId>
        <version>1.0</version>
        <scope>eclipse</scope>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>cascading</groupId>
            <artifactId>cascading-core</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>cascading</groupId>
            <artifactId>cascading-test</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
</dependencyManagement>
`

HI,

I am trying to creating executable jar using maven. The jar is created and when i try to execute java -jar myjar i get the following error

Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.conf.Configuration
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

I extracted the jar and see that hadoop classes are not present. And in the logs i could see that hadoop jars are not copied .where is the problem

My pom.xml is as follows :

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.MyClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

    <!-- Compile time dependencies -->
<dependency>
  <groupId>bixo</groupId>
 <artifactId>bixo-core</artifactId>
     <version>1.0-SNAPSHOT</version>
</dependency>   
    <dependency>
        <groupId>cascading</groupId>
        <artifactId>cascading-core</artifactId>
    </dependency>

    <dependency>
        <groupId>cascading</groupId>
        <artifactId>cascading-11-jdbc</artifactId>
        <version>0.0.4</version>
    </dependency>

    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.7</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.0.1</version>
    </dependency>

 <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-parsers</artifactId>
        <version>0.8</version>
    </dependency>

    <dependency>
        <groupId>org.archive</groupId>
        <artifactId>heritrix</artifactId>
        <version>1.14.3</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.4</version>
    </dependency>

    <dependency>
        <groupId>org.kohsuke</groupId>
        <artifactId>args4j</artifactId>
        <version>2.0.10</version>
    </dependency>


    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-core</artifactId>
        <version>0.21.0-SNAPSHOT</version>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <artifactId>jetty</artifactId>
                <groupId>org.mortbay.jetty</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jetty-util</artifactId>
                <groupId>org.mortbay.jetty</groupId>
            </exclusion>
        </exclusions>
    </dependency>
  <dependency> 
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapred</artifactId>
            <version>0.21.0-SNAPSHOT</version>
            <scope>provided</scope>
</dependency>
 <dependency> 
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>0.21.0-SNAPSHOT</version>
            <scope>provided</scope>
</dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <!-- Eclipse project generation dependencies -->

    <dependency>
        <groupId>ant-eclipse</groupId>
        <artifactId>ant-eclipse-jvm1.2</artifactId>
        <version>1.0</version>
        <scope>eclipse</scope>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>cascading</groupId>
            <artifactId>cascading-core</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>cascading</groupId>
            <artifactId>cascading-test</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
</dependencyManagement>
`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2024-10-25 02:10:28

您已将 hadoop jar 指定为范围为 provided 的依赖项。
这意味着它们将在运行时位于类路径上,如果您希望将它们包含在 jar 中,请删除范围标签并接受 compile 的默认范围。

请参阅 http://maven.apache.org/pom.html

You've specified the hadoop jars as dependencies with a scope of <scope>provided</scope>.
This implies that they will be on the classpath at runtime, if you want them to be included in your jar, then remove the scope tags and accept the default scope of compile.

see http://maven.apache.org/pom.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文