ANTLR4 CharStream class missing



我正在使用ANTLR4来解析我的Java Shell项目的命令行。 当我在VSCode中运行JUnit测试时,一切都很好。但是,当我构建 Docker 映像并尝试以交互模式运行 shell 时,出现此错误:

错误:无法初始化主类 uk.ac.ucl.jsh.Jsh

原因:java.lang.NoClassDefFoundError: org/antlr/v4/runtime/CharStream

我的pom.xml文件有任何问题,还是问题来自其他地方?这是我的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>uk.ac.ucl.jsh</groupId>
<artifactId>jsh</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>jsh</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<argLine>-XX:MaxPermSize=512m</argLine>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>uk.ac.ucl.jsh.Jsh</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7.2</version>         
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>            
</execution>
</executions>
<configuration>
<visitor>true</visitor>
<listener>true</listener>
<outputDirectory>
${basedir}/src/main/java/
</outputDirectory>
</configuration>    
</plugin>          
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<!-- select non-aggregate reports -->
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>

这看起来不像是ANTLR问题。 您需要了解如何进行构建以及如何将 Docker 映像放在一起。

对此有几种解决方案。

  • 研究如何让 Maven 生成一个 Uber jar(即一个捆绑所有依赖项的 jar 文件(
  • 确保 Docker 映像包含 ANTLR 运行时并在类路径中。

其中第一个可能是当今更"正常"的解决方案。

相关内容

最新更新