2016-01-27 125 views
1

所以我創建了一個獨立的應用程序,它生成一個jar。當我從Eclipse運行它時,它運行,「運行」 - > Java應用程序。爲maven生成一個jar

當我嘗試從控制檯運行它時,我開始從swt中得到問題,所以我將其添加到我的清單swt.jar中,並開始解決下一個依賴於我的pom的問題。

這裏是我的pom.xml與SWT.JAR

Manifest-Version: 1.0 
Main-Class: mapper.osm.main.Main 
Class-Path: swt.jar 

一個例子,我下載的SWT和複製它在我的罐子的同一文件夾中。但我不想將所有我的庫手動添加到我的清單。感覺應該有一個更簡單,更麻煩的方式來做到這一點。

這是我加入SWT

$ java -jar mapper.jar 
java.lang.NoClassDefFoundError: org/opengis/geometry/coordinate/Position 
     at java.lang.Class.getDeclaredMethods0(Native Method) 
     at java.lang.Class.privateGetDeclaredMethods(Unknown Source) 
     at java.lang.Class.privateGetMethodRecursive(Unknown Source) 
     at java.lang.Class.getMethod0(Unknown Source) 
     at java.lang.Class.getMethod(Unknown Source) 
     at sun.launcher.LauncherHelper.validateMainClass(Unknown Source) 
     at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) 
Caused by: java.lang.ClassNotFoundException: org.opengis.geometry.coordinate.Position 
     at java.net.URLClassLoader.findClass(Unknown Source) 
     at java.lang.ClassLoader.loadClass(Unknown Source) 
     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
     at java.lang.ClassLoader.loadClass(Unknown Source) 
     ... 7 more 
Error: A JNI error has occurred, please check your installation and try again 
Exception in thread "main" 

這是完全一樣的前之後,但與一些SWT圖形庫得到了錯誤。 這是我的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>mapper</groupId> 
    <artifactId>mapper.osm</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>mapper,osm</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <geotools.version>13.2</geotools.version> 
    </properties> 

    <dependencies> 
     <!-- Eclipse's JFace extensions --> 
     <dependency> 
      <groupId>org.eclipse.ui</groupId> 
      <artifactId>org.eclipse.ui.workbench</artifactId> 
      <version>3.7.1.v20120104-1859</version> 
     </dependency> 
     <!-- SWT --> 
     <dependency> 
      <groupId>org.eclipse.swt</groupId> 
      <artifactId>${swt.artifactId}</artifactId> 
      <version>4.4</version> 
      <scope>compile</scope> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.geotools</groupId> 
      <artifactId>gt-shapefile</artifactId> 
      <version>${geotools.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.geotools</groupId> 
      <artifactId>gt-swing</artifactId> 
      <version>${geotools.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.vividsolutions</groupId> 
      <artifactId>jts</artifactId> 
      <version>1.12</version> 
     </dependency> 
     <dependency> 
      <groupId>joda-time</groupId> 
      <artifactId>joda-time</artifactId> 
      <version>2.8.2</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-io</groupId> 
      <artifactId>commons-io</artifactId> 
      <version>2.4</version> 
     </dependency> 
     <!-- RXTX --> 
     <dependency> 
      <groupId>org.bidib.jbidib.org.qbang.rxtx</groupId> 
      <artifactId>rxtxcomm</artifactId> 
      <version>2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>com.itextpdf</groupId> 
      <artifactId>itextpdf</artifactId> 
      <version>5.5.8</version> 
     </dependency> 

     <dependency> 
      <groupId>xerces</groupId> 
      <artifactId>xercesImpl</artifactId> 
      <version>2.11.0</version> 
     </dependency> 

     <!-- Instalador izpack --> 
     <dependency> 
      <groupId>org.codehaus.izpack</groupId> 
      <artifactId>izpack-maven-plugin</artifactId> 
      <version>5.0.6</version> 
      <scope>provided</scope> 
     </dependency> 


    </dependencies> 

    <repositories> 
     <repository> 
      <id>swt-repo</id> 
      <url>https://swt-repo.googlecode.com/svn/repo/</url> 
     </repository> 
     <repository> 
      <id>maven2-repository.dev.java.net</id> 
      <name>Java.net repository</name> 
      <url>http://download.java.net/maven/2</url> 
     </repository> 
     <repository> 
      <id>osgeo</id> 
      <name>Open Source Geospatial Foundation Repository</name> 
      <url>http://download.osgeo.org/webdav/geotools/</url> 
     </repository> 
    </repositories> 
    <build> 
     <finalName>mapper</finalName> 
     <plugins> 
      <plugin> 
       <!-- Build an executable JAR --> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-jar-plugin</artifactId> 
       <version>2.6</version> 
       <configuration> 
        <archive> 
         <manifest> 
          <mainClass>mapper.osm.main.Main</mainClass> 
         </manifest> 
        </archive> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.3</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
        <encoding>UTF-8</encoding> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>com.zenjava</groupId> 
       <artifactId>javafx-maven-plugin</artifactId> 
       <version>2.0</version> 
       <configuration> 
        <mainClass>org.korecky.myjavafx.App</mainClass> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

    <profiles> 
     <profile> 
      <id>unix-amd64</id> 
      <activation> 
       <os> 
        <family>unix</family> 
        <arch>amd64</arch> 
       </os> 
      </activation> 
      <properties> 
       <swt.artifactId>org.eclipse.swt.gtk.linux.x86_64</swt.artifactId> 
      </properties> 
     </profile> 
     <profile> 
      <id>windows-x86</id> 
      <activation> 
       <os> 
        <family>windows</family> 
        <arch>x86</arch> 
       </os> 
      </activation> 
      <properties> 
       <swt.artifactId>org.eclipse.swt.win32.win32.x86</swt.artifactId> 
      </properties> 
     </profile> 
     <profile> 
      <id>windows64</id> 
      <activation> 
       <os> 
        <family>dos</family> 
        <arch>amd64</arch> 
       </os> 
      </activation> 
      <properties> 
       <swt.artifactId>org.eclipse.swt.win32.win32.x86_64</swt.artifactId> 
      </properties> 
     </profile> 
     <profile> 
      <id>windows-x86_64</id> 
      <activation> 
       <os> 
        <family>windows</family> 
        <arch>x86_64</arch> 
       </os> 
      </activation> 
      <properties> 
       <swt.artifactId>org.eclipse.swt.win32.win32.x86_64</swt.artifactId> 
      </properties> 
     </profile> 
     <profile> 
      <id>macosx-x86</id> 
      <activation> 
       <os> 
        <family>mac</family> 
        <arch>x86</arch> 
       </os> 
      </activation> 
      <properties> 
       <swt.artifactId>org.eclipse.swt.cocoa.macosx</swt.artifactId> 
      </properties> 
     </profile> 
     <profile> 
      <id>macosx-x86_64</id> 
      <activation> 
       <os> 
        <family>mac</family> 
        <arch>x86_64</arch> 
       </os> 
      </activation> 
      <properties> 
       <swt.artifactId>org.eclipse.swt.cocoa.macosx.x86_64</swt.artifactId> 
      </properties> 
     </profile> 
    </profiles> 
</project> 
+0

這是一個類路徑錯誤。需要在classpath中包含所需的類/ jar。看看http://stackoverflow.com/questions/22605831/how-to-resolve-this-error-caused-by-java-lang-classnotfoundexception –

+0

我明白,但我問的是有更好的方式這樣做是因爲我使用maven,而maven正在從各自的回購庫中提取所有庫。 –

+0

你目前的班級路徑是什麼?此外,我假設所有必需的類文件都在給定的罐子裏 –

回答

1

嘗試使用Maven的依賴插件的罐子,和Maven Jar插件與「addClasspath」節點複製到依賴,加入到清單:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <executions> 
     <execution> 
      <id>copy-dependencies</id> 
      <phase>package</phase> 
      <goals> 
       <goal>copy-dependencies</goal> 
      </goals> 
      <configuration> 
       <outputDirectory>${project.build.directory}/lib</outputDirectory> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <configuration> 
     <archive> 
      <manifest> 
       <addClasspath>true</addClasspath> 
       <classpathPrefix>lib</classpathPrefix> 
       <mainClass>my.test.App</mainClass> 
      </manifest> 
     </archive> 
    </configuration> 
</plugin> 

這會將jar複製到target/lib並將它們添加到清單中,其前綴爲lib/