2016-09-04 3 views
0

Я убежищем проект Maven использованием Antlr4.5 со следующей структурой:Antlr4 с образцом проекта Maven

src/main/antlr4/ArrayInit.g4 
src/main/java/Test.java 
pom.xml 

Test.java:

import org.antlr.v4.runtime.*; 
import org.antlr.v4.runtime.tree.*; 
public class Test { 
    public static void main(String[] args) throws Exception { 
     ANTLRInputStream input = new ANTLRInputStream(System.in); 
     ArrayInitLexer lexer = new ArrayInitLexer(input); 
     CommonTokenStream tokens = new CommonTokenStream(lexer); 
     ArrayInitParser parser = new ArrayInitParser(tokens); 
     ParseTree tree = parser.init(); // begin parsing at init rule 
     System.out.println(tree.toStringTree(parser)); // print LISP-style tree 
    } 
} 

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>antlrtest</groupId> 
    <artifactId>app1</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.antlr</groupId> 
       <artifactId>antlr4-maven-plugin</artifactId> 
       <version>4.5</version> 
       <executions> 
        <execution> 
         <id>antlr</id> 
         <goals> 
          <goal>antlr4</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

    <dependencies> 
     <dependency> 
      <groupId>org.antlr</groupId> 
      <artifactId>antlr4-runtime</artifactId> 
      <version>4.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.antlr</groupId> 
      <artifactId>antlr4-maven-plugin</artifactId> 
      <version>4.5</version> 
      <type>maven-plugin</type> 
     </dependency> 
    </dependencies> 

</project> 

Компиляция с

mvn package 

Успешно, а созданные в antlr источники и соответствующие классы создаются в целевых классах.

Однако работает сгенерированного банку с

java -cp target/app1-1.0-SNAPSHOT.jar Test 

имеет следующее сообщение об ошибке, как результат:

Error: A JNI error has occurred, please check your installation and 

try again 
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/v4/runtime/CharStream 
    at java.lang.Class.getDeclaredMethods0(Native Method) 
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) 
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048) 
    at java.lang.Class.getMethod0(Class.java:3018) 
    at java.lang.Class.getMethod(Class.java:1784) 
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544) 
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) 
Caused by: java.lang.ClassNotFoundException: org.antlr.v4.runtime.CharStream 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 7 more 

Что я пропустил?

ответ

2

Похоже, что ANTLR не входит в банку. Нужно добавить его через classpath или сказать Maven, чтобы включить его зависимости, поместив их в файл jar.

Включит зависимости с сборочным плагин:

 <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <archive> 
        <manifest> 
         <mainClass>Test</mainClass> 
        </manifest> 
       </archive> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
      </configuration> 
      <executions> 
       <execution> 
        <id>make-assembly</id> <!-- this is used for inheritance merges --> 
        <phase>package</phase> <!-- bind to the packaging phase --> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

Затем он может быть составлен с использованием

mvn clean package 

или

mvn clean compile assembly:single 

Он может быть выполнен с использованием

java -jar target/app1-1.0-SNAPSHOT-jar-with-dependencies.jar 
Смежные вопросы