簡體   English   中英

無法使用Maven項目創建可運行的jar文件

[英]unable to create runnable jar file with maven project

我從.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使用干凈的程序包組裝進行構建:單個,但這沒有用。 因此決定放棄並返回調試模式,但現在項目將無法運行:

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_home

jre安裝

設置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”並下載。
然后轉到下圖所示的設置,然后單擊添加... ,然后選擇Standart VM ,單擊下一步,然后指定jdk的路徑。 在此處輸入圖片說明

切換到陰影插件 與程序集插件相比,它更易於使用和得到更好的支持。 另外,它不需要您運行單獨的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM