【问题标题】:unable to create runnable jar file with maven project无法使用 maven 项目创建可运行的 jar 文件
【发布时间】:2015-02-06 05:50:46
【问题描述】:

我从 .NET 到 java 的过渡充满了问题,尤其是使用 SPring Tool Suite。我创建了一个简单的 Maven 项目,因为每个人都告诉我这是最好的方式,因为 Maven 会下载所有需要的库并将它们打包。在无休止的错误和尝试修复之后,我让项目工作并通过 Spring 以某种调试模式运行它。试图创建一个可运行的 jar,这就是问题开始的地方。第一个 jar 不适用于此错误:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
    Caused by: org.apache.hadoop.hbase.client.NoServerForRegionException: 
    Unable to find region for  after 35 tries.
    at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.
    locateRegionInMeta(ConnectionManager.java:1251)
    at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.
     locateRegion(ConnectionManager.java:1128)
    at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.
    locateRegion(ConnectionManager.java:1111)
    at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegion
    (ConnectionManager.java:1070)
    at org.apache.hadoop.hbase.client.HTable.finishSetup(HTable.java:347)
    at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:201)
    at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:159)
    at com.boardreader.hbase.HBaseMain.main(HBaseMain.java:148)
    ... 5 more

找到了一些指示我检查 jre 版本和 jdk 等的资源,并将以下内容添加到 pom 文件中以创建显示主类的清单。

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>com.myproject.deduper.HBaseMain</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  </plugin>
 </plugins>

然后告诉 maven build with clean package assembly:single 但这不起作用。所以决定放弃并回到调试模式,但现在项目将无法运行:

cannot find main class HBaseMain

现在真的很沮丧。从 pom 文件中删除条目,重建和清理不起作用。调试或作为 java 应用程序运行不起作用。关闭 Spring,然后重新打开并清理项目,最后在调试模式下工作。我需要让它工作以部署到另一台服务器,但为什么这很难完成。对于所有微软的不断抱怨,我只是尝试部署一个项目没有任何问题。

编辑:

更换插件

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
    <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>
            <filters>
                <filter>
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>META-INF/*.SF</exclude>
                        <exclude>META-INF/*.DSA</exclude>
                        <exclude>META-INF/*.RSA</exclude>
                        <exclude>LICENSE</exclude>
                        <exclude>LICENSE.txt</exclude>
                        <exclude>NOTICE.txt</exclude>
                    </excludes>
                </filter>
            </filters>
            <transformers>
                <transformer  implementation="org.apache.maven.plugins.shade.resource.
                        ManifestResourceTransformer">
                    <mainClass>com.myproject.deduper.HBaseMain</mainClass>
                </transformer>
            </transformers>
        </configuration>
    </execution>
</executions>
 </plugin>

现在错误:

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.338 s
[INFO] Finished at: 2014-12-08T09:54:30-05:00
[INFO] Final Memory: 13M/244M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:
compile   (default-compile) on project Hbase-t: Fatal error compiling: tools.jar not 
found: C:\Program Files\Java\jre7\..\lib\tools.jar -> [Help 1]
[ERROR]

【问题讨论】:

    标签: java spring maven


    【解决方案1】:

    切换到shade plugin。它比组装插件更容易使用和更好的支持。另外,它不需要您运行单独的 Maven 目标。它挂接到包并安装。这应该为你做:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                                <exclude>LICENSE</exclude>
                                <exclude>LICENSE.txt</exclude>
                                <exclude>NOTICE.txt</exclude>
                            </excludes>
                        </filter>
                    </filters>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>com.x.y.z.Test</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

      【解决方案2】:

      设置 JAVA_HOME 指向 jdk(不是 jre)

      JAVA_HOME=C:\jdk1.7.0_71
      

      添加这个插件

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <configuration>
              <archive>
                  <manifest>
                      <addClasspath>true</addClasspath>
                      <mainClass>com.company.Main</mainClass>
                  </manifest>
              </archive>
          </configuration>
      </plugin>
      

      配置eclipse使用jdk:首先你的电脑上必须有jdk,如果没有,输入google“download jdk”并下载。
      然后进入下图设置,点击Add...,然后选择Standart VM,点击next, 并指定jdk的路径。

      【讨论】:

      • 你缺少 tools.jar,它是 jdk 的一部分,所以你必须使用 jre
      • 我需要更改上面的设置吗?在第二张图片中?
      • 安装的JRE下没有添加jdk的选项
      • 上面写着 JRE,但是你应该指定 JDK 的路径,看图片
      猜你喜欢
      • 2016-07-20
      • 1970-01-01
      • 2012-11-01
      • 2012-01-24
      • 2014-04-01
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多