2017-09-26 63 views
0

我在Eclipse中有簡單的maven項目。該應用程序包含文件:src/main/resources/test.txt。我想讀取該文件並將其寫入輸出中。它在Eclipse中工作,但在創建jar文件後不能從命令行中運行。如何配置項目以兩種方式工作。 我錯在哪裏? 不正確的jar文件,java代碼,清單,maven配置?通過maven創建jar來從同一個jar文件讀取資源文本文件

應用程序結構:

Application structure

運行:

java -jar fileRead-0.0.1-SNAPSHOT.jar 

錯誤:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils 
    at fileRead.FileRead.main(FileRead.java:11) 
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
     ... 1 more 

Java代碼:

package fileRead; 
import java.io.IOException; 

import org.apache.commons.io.IOUtils; 

public class FileRead { 
    public static void main(String[] args) { 
    String file = null; 
    try { 
     file = IOUtils.toString((new FileRead()).getClass().getClassLoader().getResourceAsStream("test.txt")); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    System.out.println(file); 
    } 
} 

的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>com.fileRead</groupId> 
<artifactId>fileRead</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>jar</packaging> 
<name>fileRead</name> 
<url>http://maven.apache.org</url> 
<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    <maven.compiler.source>1.8</maven.compiler.source> 
</properties> 
<dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.12</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.directory.studio</groupId> 
     <artifactId>org.apache.commons.io</artifactId> 
     <version>2.4</version> 
    </dependency> 
</dependencies> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>3.0.2</version> 
      <configuration> 
       <archive> 
        <manifest> 
         <addClasspath>true</addClasspath> 
         <mainClass>fileRead.FileRead</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

FILEREAD-0.0.1-SNAPSHOT.jar:

fileRead 
    FileRead.class 
META-INF 
    maven 
    MANIFEST.MF 
    test.txt 

MANIFEST.MF:

Manifest-Version: 1.0 
Built-By: msmorenda 
Class-Path: org.apache.commons.io-2.4.jar commons-io-2.4.jar 
Created-By: Apache Maven 3.5.0 
Build-Jdk: 1.8.0_121 
Main-Class: fileRead.FileRead 

的test.txt:

abc 
def 
ghi 
+0

你爲什麼要編譯jar而不是java類?我認爲maven有一個特定的結構,當你創建jar文件時,沒有名爲「resources」或「main/java」的目錄,所以我沒有找到它 –

+0

當你說「它在Eclipse中工作時,但在創建jar文件後不能從命令行得到「 - 你的意思是它可以在Eclipse中工作,但是當你在服務器上運行JAR文件時,Tomcat或類似的東西時不會運行? – SHG

+0

我在Windows上沒有服務器只是命令行。 – maly174

回答

0

的問題是,MANIFEST.MF指向所需的依賴關係。但是依賴關係與生成的jar文件不在同一個目錄中。要複製所需的庫(公共-IO),你需要另一個插件添加到您的pom如下:

<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}/lib</outputDirectory> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <version>3.0.2</version> 
    <configuration> 
     <archive> 
      <manifest> 
       <classpathPrefix>lib</classpathPrefix> 
       <addClasspath>true</addClasspath> 
       <mainClass>fileread.FileRead</mainClass> 
      </manifest> 
     </archive> 
    </configuration> 
</plugin> 

maven-dependency-plugin具有上述配置將在lib目錄複製target下所需的庫。現在,如果您將target目錄設置爲當前目錄,並致電java -jar <name_of_jar>,那麼所有內容都應該有效。

注意:如果您想將您的jar文件移動到另一個位置,您應該將其與lib目錄一起打包。並且請注意,我將您班級的包名改爲fileread而不是fileRead。原因? Java約定。

-1

可以用行家它包含你需要的庫創建的組件,然後使用「MVN包」來創建它

的pom.xml

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.6</version> 
      <configuration> 
       <descriptors> 
        <descriptor>src/assemblies/bin-with-dependencies.xml</descriptor> 
       </descriptors> 
       <tarLongFileMode>gnu</tarLongFileMode> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

SRC /組件/濱與 - dependencies.xml

<assembly> 
    <id>bin-with-dependencies</id> 
    <formats> 
     <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <dependencySets> 
     <dependencySet> 
      <outputDirectory></outputDirectory> 
      <unpack>false</unpack> 
      <scope>compile</scope> 
     </dependencySet> 
     <dependencySet> 
      <unpack>false</unpack> 
      <scope>runtime</scope> 
      <useProjectArtifact>true</useProjectArtifact> 
     </dependencySet> 
    </dependencySets> 
</assembly> 

這將創建幷包含與您的JAR文件夾歸檔和它的所有庫 ,然後運行它像

java命令{directory_jars}/* fileread.FileRead ...

「/ *」 可能僅適用於Windows