1

I got this error message have been browsing for a long time and still couldn't find the reason or the fix for it.

Here is the Error message I am getting:

Error: Unable to initialize main class app.ExpressionApp Caused by: java.lang.NoClassDefFoundError: org/antlr/v4/runtime/tree/ParseTree

Here is the image of my directories (the folder I am trying to run is the visitor one):

enter image description here

And here is the ExpressionApp class content

package app;

import java.io.IOException;

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

import Expression.AntlrToProgram;
import Expression.ExpressionProcessor;
import Expression.Program;
import antlr.ExprLexer;
import antlr.ExprParser;

public class ExpressionApp {

    public static void main(String[] args) {
        if(args.length != 1) {
            System.err.print("Usage: file name");
        }
        else {
            String fileName = args[0];
            ExprParser parser = getParser(fileName);
            
            ParseTree antlrAST = parser.prog();
            
            AntlrToProgram progVisitor = new AntlrToProgram();
            Program prog = progVisitor.visit(antlrAST);
            
            if(progVisitor.semanticErrors.isEmpty()) {
                ExpressionProcessor ep = new ExpressionProcessor(prog.expressions);
                for(String evaluation: ep.getEvaluationResults()) {
                    System.out.println(evaluation);
                }
            }
            else {
                for(String err: progVisitor.semanticErrors) {
                    System.out.println(err); 
                }
            }
        }

    }

    private static ExprParser getParser(String fileName) {
        ExprParser parser = null;
        
        try {
            CharStream input = CharStreams.fromFileName(fileName);
            ExprLexer lexer = new ExprLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new ExprParser(tokens);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return parser;
    }
}

I have checked whether I have imported any wrong antlr files and nothing was foud

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
HARPREET
  • 11
  • 2
  • It appears that your ANTLR jar is not in the classpath at runtime (this is not the same as being in the classpath at compile time, which had to be correct to get a clean compile) – Mike Cargal Dec 07 '21 at 02:28
  • Hi @HARPREET above issue occurs when having class conflict during runtime. this issue can occur either 2 jars having same name and during compilation 1st jar pointed with correct import and during runtime it's referring to another path. Please do clean and extract jar to see actual class is available on requested path or not. – Raushan Kumar Dec 07 '21 at 06:11
  • Hello @RaushanKumar I did clean and export jar and then tried to execute it in the command prompt and got the following error: Error: LinkageError occurred while loading main class app.ExpressionApp java.lang.UnsupportedClassVersionError: app/ExpressionApp has been compiled by a more recent version of the Java Runtime (class file version 60.0), this version of the Java Runtime only recognizes class file versions up to 55.0. I recently downloaded the java file that allowed me to use javac, however I already had an java file with jre, is this the one that I might need to updata? – HARPREET Dec 08 '21 at 07:38
  • yeah, you just need to set the current java version to 55.0, then it will work. Please go through the below post. https://stackoverflow.com/questions/9170832/list-of-java-class-file-format-major-version-numbers – Raushan Kumar Dec 08 '21 at 07:43

0 Answers0