2016-06-02 6 views
0

Könnten Sie mir bitte erklären, warum meine JAR-Datei nicht außerhalb des Zielordners ausgeführt wird? Und wie kann ich das unabhängig machen? Kopieren/Einfügen in ein anderes VerzeichnisWie mache ich JAR-Datei unabhängig?

Wenn ich meine jar außerhalb des Zielordners ausführen, wird NoClassDefFound Fehler erzeugt. Das Laden von Gläsern aus Abhängigkeiten ist nicht möglich.

Das ist mein pom.xml:

<properties> 
     <docx4j.version>3.3.0</docx4j.version> 
    </properties> 

    <build> 
     <plugins> 
      <plugin> 
       <!-- Build an executable JAR --> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-jar-plugin</artifactId> 
       <version>2.6</version> 
       <configuration> 
        <archive> 
         <manifest> 
          <addClasspath>true</addClasspath> 
          <classpathPrefix>lib/</classpathPrefix> 
          <mainClass>com.epam.executor.Main</mainClass> 
         </manifest> 
        </archive> 
       </configuration> 
      </plugin> 
      <plugin> 
       <!-- Build an executable JAR with runtime dependencies so that this program can be executed from command line using java -jar command --> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.10</version> 
       <executions> 
        <execution> 
         <id>copy-dependencies</id> 
         <phase>package</phase> 
         <goals> 
          <goal>copy-dependencies</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${project.build.directory}/lib</outputDirectory> 
          <overWriteReleases>false</overWriteReleases> 
          <overWriteSnapshots>false</overWriteSnapshots> 
          <overWriteIfNewer>true</overWriteIfNewer> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

    <dependencies> 
     <dependency> 
      <groupId>org.docx4j</groupId> 
      <artifactId>docx4j</artifactId> 
      <version>${docx4j.version}</version> 
     </dependency> 
    </dependencies> 

I mit 3 Parametern mein Glas über diesen Befehl ausführen:

java -jar DocumentTemplate-1.0.jar D:\Trash\xml1.xml D:\Trash\template.docx D:\Trash\results.docx 
+1

Meine Vermutung ist, dass Sie 'docx4j' brauchen, also sollten Sie es Ihrem Klassenpfad hinzufügen. Sie haben 'lib /' hinzugefügt und das muss alle Gläser enthalten, auf die Sie angewiesen sind. (Relativ zum Glas) –

+0

Kurz gesagt, wo ist docx4j? und was ist die Fehlermeldung, die Sie bekommen? –

Antwort

1

Ich denke, Sie müssen ein Jar erstellen, das alle seine ausführbaren Abhängigkeiten auch enthält. Ich bin du ein Ein-Jar-Plugin dafür gewesen. versuche es unter dem Plugin.

<plugin> 
    <!-- Build an executable JAR --> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <version>2.6</version> 
    <configuration> 
     <archive> 
     <manifest> 
      <addClasspath>true</addClasspath> 
      <classpathPrefix>lib/</classpathPrefix> 
      <mainClass>com.kulhade.elasticsearch.Indexer</mainClass> 
     </manifest> 
     </archive> 
    </configuration> 
    </plugin> 
    <plugin> 
    <groupId>org.dstovall</groupId> 
    <artifactId>onejar-maven-plugin</artifactId> 
    <version>1.4.4</version> 
    <executions> 
     <execution> 
     <goals> 
      <goal>one-jar</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 
0

Kopieren Abhängigkeiten in ein bestimmtes Verzeichnis

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <executions> 
    <execution> 
     <id>copy-dependencies</id> 
     <phase>prepare-package</phase> 
     <goals> 
     <goal>copy-dependencies</goal> 
     </goals> 
     <configuration> 
     <outputDirectory>${project.build.directory}/${project.build.finalName}.lib</outputDirectory> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

Fabrikat Das Jar Executable und Classpath Aware

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <configuration> 
    <archive> 
     <manifest> 
     <addClasspath>true</addClasspath> 
     <classpathPrefix>${project.build.finalName}.lib/</classpathPrefix> 
     <mainClass>${fully.qualified.main.class}</mainClass> 
     </manifest> 
    </archive> 
    </configuration> 
</plugin> 

Zu diesem Zeitpunkt ist das Jar tatsächlich mit externen Klassenpfadelementen ausführbar.

$ java -jar target/${project.build.finalName}.jar