maven找不到apache randomstringgenerator

1dkrff03  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(400)

我正在尝试使用新的apachecommons文本新随机字符串生成器,但是在internet上找不到任何关于如何正确导入它的用法。maven构建成功,但是当我尝试使用以下命令从命令行运行jar文件时

java -cp target/my-app-1.0.jar com.mycompany.app.App

…我得到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/text/RandomStringGenerator$Builder
        at com.mycompany.app.App.main(App.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.text.RandomStringGenerator$Builder
        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)
        ... 1 more

在构建过程中,我还收到以下警告:

The POM for org.apache.commons:commons-text:jar:1.1 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more detail

以下是maven调试版本:

[WARNING] The POM for org.apache.commons:commons-text:jar:1.1 is invalid, transitive dependencies (if any) will not be available: 1 problem was encountered while building the effective model
[FATAL] Non-parseable POM C:\Users\me\.m2\repository\org\apache\commons\commons-text\1.1\commons-text-1.1.pom: only whitespace content allowed before start tag and not a (position: START_DOCUMENT seen a... @1:1)  @ line 1, column 1

这是我的 App.java 代码:

package com.mycompany.app;
import org.apache.commons.text.RandomStringGenerator;
public class App {
    public static void main(String[] args) {
        // Generates a 20 code point string, using only the letters a-z
        RandomStringGenerator generator = new RandomStringGenerator.Builder()
            .withinRange('a', 'z').build();
        String randomLetters = generator.generate(20);
        System.out.println( randomLetters );
    }
}

这是我的 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-text</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
hjqgdpho

hjqgdpho1#

如果您想从一个jar运行您的应用程序,您将需要一个分解的jar。默认的类加载器不会从您的jar中找到apachejar。
您可以使用shade插件创建一个分解的jar。将下面的内容添加到 pom.xml :

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>com.mycompany.app.App</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

然后,可以运行以下命令:

java -jar target/my-app-1.0.jar

相关问题