我在Eclipse中有简单的maven项目。该应用程序包含文件:src / main / resources / test.txt。我想读取文件并将其写在输出上。它可以在Eclipse中运行,但在创建jar文件后不能从命令行运行。如何配置项目以两种方式工作。 我哪里错了? 不正确的jar文件,java代码,清单,maven配置?
申请结构:
运行:
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 :(得分:0)
问题是MANIFEST.MF
指向所需的依赖项。但是依赖项与生成的jar文件不在同一目录中。要复制所需的库(commons-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 :(得分:-1)
您可以使用maven创建一个包含所需库的程序集,然后使用“mvn package”创建它
的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 /组件/ bin中与 - 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 -cp {directory_jars} / * fileread.FileRead ...
“/ *”可能仅适用于Windows