gpt4 book ai didi

java - picocli 不适用于 cmd.exe 中的 jline3

转载 作者:行者123 更新时间:2023-11-30 01:55:48 28 4
gpt4 key购买 nike

我想将 picocli 与 jline3 一起使用。因此,我使用以下 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.flaxel</groupId>
<artifactId>picocli_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>picocli</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

<picocli.version>3.9.3</picocli.version>
<jline.version>3.9.0</jline.version>
</properties>

<dependencies>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>${picocli.version}</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli-shell-jline3</artifactId>
<version>${picocli.version}</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>
<mainClass>com.flaxel.picocli.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compile-plugin</artifactId>
<version>3.7.0</version>
</plugin>
</plugins>
</build>

现在我已经从 picocli github page 复制了一个类:

package com.flaxel.picocli;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.MaskingCallback;
import org.jline.reader.ParsedLine;
import org.jline.reader.UserInterruptException;
import org.jline.reader.impl.DefaultParser;
import org.jline.reader.impl.LineReaderImpl;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParentCommand;
import picocli.shell.jline3.PicocliJLineCompleter;

/**
* Example that demonstrates how to build an interactive shell with JLine3 and
* picocli.
*
* @since 3.9
*/
public class App {

/**
* Top-level command that just prints help.
*/
@Command(name = "", description = "Example interactive shell with completion", footer = { "",
"Press Ctl-D to exit." }, subcommands = { MyCommand.class, ClearScreen.class })
static class CliCommands implements Runnable {
LineReaderImpl reader;
PrintWriter out;

CliCommands() {
}

public void setReader(LineReader reader) {
this.reader = (LineReaderImpl) reader;
out = reader.getTerminal().writer();
}

@Override
public void run() {
out.println(new CommandLine(this).getUsageMessage());
}
}

/**
* A command with some options to demonstrate completion.
*/
@Command(name = "cmd", mixinStandardHelpOptions = true, version = "1.0", description = "Command with some options to demonstrate TAB-completion"
+ " (note that enum values also get completed)")
static class MyCommand implements Runnable {
@Option(names = { "-v", "--verbose" })
private boolean[] verbosity = {};

@Option(names = { "-d", "--duration" })
private int amount;

@Option(names = { "-u", "--timeUnit" })
private TimeUnit unit;

@ParentCommand
CliCommands parent;

@Override
public void run() {
if (verbosity.length > 0) {
parent.out.printf("Hi there. You asked for %d %s.%n", amount, unit);
} else {
parent.out.println("hi!");
}
}
}

/**
* Command that clears the screen.
*/
@Command(name = "cls", aliases = "clear", mixinStandardHelpOptions = true, description = "Clears the screen", version = "1.0")
static class ClearScreen implements Callable<Void> {

@ParentCommand
CliCommands parent;

@Override
public Void call() throws IOException {
parent.reader.clearScreen();
return null;
}
}

public static void main(String[] args) {
try {
// set up the completion
CliCommands commands = new CliCommands();
CommandLine cmd = new CommandLine(commands);
Terminal terminal = TerminalBuilder.builder().build();
LineReader reader = LineReaderBuilder.builder()
.terminal(terminal)
.completer(new PicocliJLineCompleter(cmd.getCommandSpec()))
.parser(new DefaultParser())
.build();
commands.setReader(reader);
String prompt = "prompt> ";
String rightPrompt = null;

// start the shell and process input until the user quits with Ctrl-D
String line;
while (true) {
try {
line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
ParsedLine pl = reader.getParser().parse(line, 0);
String[] arguments = pl.words().toArray(new String[0]);
CommandLine.run(commands, arguments);
} catch (UserInterruptException e) {
// Ignore
} catch (EndOfFileException e) {
return;
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}

如果我在 Eclipse IDE 中运行代码,我可以在控制台中编写命令并得到答案。但是如果我用maven创建一个jar文件,它就不起作用。我收到以下错误:

Error: Unable to initialize main class com.flaxel.picocli.App Caused by: java.lang.NoClassDefFoundError: org/jline/reader/UserInterruptException

我使用 Eclipse 2018-09 和 Java 11。

最佳答案

出现 NoClassDefFoundError 是因为您在运行 jar 时缺少 JLine 类。尝试使用 Shade Maven 插件创建一个包含所有依赖项的 Uber-jar。

对于 Windows 上的 JLine 3,除了 info.picoli:picocli-shell- 之外,您可能还需要添加 org.jline:jline-terminal-jansi:3.9.0 依赖项jline3:3.9.3。如果没有这个(或jline-terminal-jna依赖项),您将得到一个没有 ANSI 颜色或自动完成功能的“哑”终端。

(info.picoli:picocli-shell-jline3:3.9.3 将引入 info.picocli:picocliorg.jline:jline code> 作为传递依赖项,因此无需显式包含这些。)

关于java - picocli 不适用于 cmd.exe 中的 jline3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54570671/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com