简体   繁体   中英

unable to create runnable jar file with maven project

My transition from .NET to java has been riddled with issues especially using SPring Tool Suite. I created a simple maven project as everyone was telling me it is the best way to go as Maven downloads all your needed libraries and packages them. After endless errors and attempts to repair I got the project to work and been running it through Spring in sort of debug mode. Tried to create a runnable jar and that is where the issues started. First jar would not work with this error:

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

Found some sources that directed me to check the jre version and jdk etc and also add the following to the pom file to create a manifest that would show the main class.

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

Then told to maven build with clean package assembly:single but this did not work. So decided to give up and go back to debug mode but now project will not run:

cannot find main class HBaseMain

Really frustrated now. Deleted entry from pom file, rebuild and clean did not work. Debug or run as java application did not work. Closed Spring and then reopened and cleaned project and finally works in debug mode. I need to get this to work to deploy to another server but why is this so hard to accomplish. For all microsofts constant complaints I did not have any issue just trying to deploy a project.

EDIT:

changed plugin

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

now error:

[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安装

Set JAVA_HOME to point to the jdk (not jre)

JAVA_HOME=C:\jdk1.7.0_71

Add this plugin

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

Configure eclipse to use jdk: first you must have jdk on your computer, if not, type in google "download jdk" and download it.
Then go to the settings like in the picture below and click Add... and then select Standart VM , click next, and specify path to the jdk. 在此处输入图片说明

Switch to the shade plugin . It's easier to use and better supported than the assembly plugin. Plus it doesn't require you to run a separate maven target. It hooks into package and install. This should do it for you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM