我正在尝试使用 Java Lanterna 为终端 GUI 运行示例代码。在 Netbeans 8.2 IDE 中运行示例成功运行(基本上是单击“播放”按钮),但从 Linux 终端运行 JAR 文件失败。
主.java:
package com.glasses.lanternatest;
import com.googlecode.lanterna.TerminalFacade;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.terminal.Terminal;
public class Main {
public static void main(String[] args) throws InterruptedException {
Screen screen = TerminalFacade.createScreen();
screen.startScreen();
screen.putString(10, 5, "Hello World!", Terminal.Color.WHITE, Terminal.Color.BLACK);
screen.refresh();
Thread.sleep(3000);
screen.stopScreen();
System.exit(0);
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.glasses</groupId>
<artifactId>LanternaTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.glasses.lanternatest.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.lanterna</groupId>
<artifactId>lanterna</artifactId>
<version>2.1.9</version>
</dependency>
</dependencies>
</project>
从终端运行生成的 JAR 文件时出现的错误:
myuser@mylaptop:~$ java -jar /home/myuser/NetBeansProjects/LanternaTest/target/LanternaTest-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/googlecode/lanterna/TerminalFacade
at com.glasses.lanternatest.Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: com.googlecode.lanterna.TerminalFacade
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 1 more
为什么通过 Linux 终端运行代码会失败?为什么com.googlecode.lanterna.TerminalFacade
从终端运行时找不到类,但在 Netbeans 8.2 中按“播放”按钮运行相同的代码时成功找到它?