在 eclipse 插件中使用 ANTLR v4 运行时

Use ANTLR v4 runtime within eclipse plugin

你好,我是 eclipse-plugin 开发的新手,我正在开始一个项目,将翻译器合并到 eclipse-plugin 中,为此我开始使用 eclipse 插件 hello word example and the grammar file provided in this example,我能够编译我的项目和 运行 插件,但是当我尝试加载解析器时出现异常 'Caused by: java.lang.NoClassDefFoundError: org/antlr/v4/runtime/CharStream',我不知道问题出在哪里,我已经测试了解析器,但在外部插件环境中工作正常。

我也在尝试合并 maven 来下载依赖项和 运行 antlr 所以我在 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>org.plugin.helloworld</groupId>
  <artifactId>org.plugin.helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4</artifactId>
        <version>4.7.1</version>
    </dependency>
  </dependencies>

  <build>
    <defaultGoal>install</defaultGoal>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.antlr</groupId>
          <artifactId>antlr4-maven-plugin</artifactId>
          <version>4.7.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-maven-plugin</artifactId>
        <version>4.7.1</version>      
        <configuration>
            <sourceDirectory>src/evaluator</sourceDirectory>
            <outputDirectory>src/evaluator</outputDirectory>
            <visitor>true</visitor>
            <listener>false</listener>
        </configuration>          
        <executions>
          <execution>
            <id>antlr</id>
            <goals>
              <goal>antlr4</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

我正在使用词法分析器和解析器,如下所示:

public class SampleHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);

        CharStream in = CharStreams.fromString("\"12*(5-6)\"");
        evaluatorLexer lexer = new evaluatorLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        evaluatorParser parser = new evaluatorParser(tokens);

        MessageDialog.openInformation(
                window.getShell(),
                "Helloworld",
                parser.eval().toString());
        return null;
    }
}

引用的库是这样的:

我设置build.properties如下

source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
               META-INF/,\
               .,\
               icons/,\
               lib/antlr4-runtime-4.7.1.jar,\
               lib/ST4-4.0.8.jar,\
               lib/antlr4-4.7.1.jar,\
               lib/antlr4-runtime-4.7.1-sources.jar

我读到有关在清单中添加为捆绑包的信息,但我在依赖项选项卡中找不到该选项,只是 org.antlr.runtime 而不是 v4。

您需要将罐子添加到 MANIFEST.MF 中的 Bundle-Classpath

在 MANIFEST.MF 编辑器的 'Runtime' 选项卡上执行此操作。在 'Classpath' 部分单击 'Add...' 并添加罐子,一定要留下 '.'代表您的正常代码的条目。

您的 Bundle-Classpath 最终应该看起来像这样:

Bundle-ClassPath: .,
 lib/antlr4-runtime-4.7.1.jar,
 lib/ST4-4.0.8.jar,
 lib/antlr4-4.7.1.jar,
 lib/antlr4-runtime-4.7.1-sources.jar