Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Could not find or load main class Test

I am currently teaching myself ANTLR and while working through the "The Definitive ANTLR Reference" book, I encountered a problem. I am doing the first example and have created the grammar file, got my Lexer and Parser Java files, created the following Java program to test it all:

import org.antlr.runtime.*;

public class Test 
{
    public static void main(String[] args) throws Exception
    {
        ANTLRInputStream input = new ANTLRInputStream(System.in);

        TLexer lexer = new TLexer(input);

        CommonTokenStream tokens = new CommonTokenStream(lexer);

        TParser parser = new TParser(tokens);

        parser.r();
    }
}

I have then used the command line to compile which works fine, then I try and run the Java class and it fails, the whole command line is shown below:

C:\Users\Dan\workspace\Test\src>dir
 Volume in drive C is Acer
 Volume Serial Number is ECB2-5E39

 Directory of C:\Users\Dan\workspace\Test\src

17/09/2012  18:15    <DIR>          .
17/09/2012  18:15    <DIR>          ..
17/09/2012  18:32               723 Test.class
16/09/2012  20:51               353 Test.java
17/09/2012  18:32             3,641 TLexer.class
15/09/2012  21:35             7,625 TLexer.java
17/09/2012  18:32             2,425 TParser.class
15/09/2012  21:35             2,115 TParser.java
               6 File(s)         16,882 bytes
               2 Dir(s)  235,276,161,024 bytes free

C:\Users\Dan\workspace\Test\src>javac -cp C:\antlr-3.4.jar TLexer.java TParser.j
ava Test.java

C:\Users\Dan\workspace\Test\src>java -cp C:\antlr-3.4.jar Test
Error: Could not find or load main class Test

C:\Users\Dan\workspace\Test\src>

I have looked a many solutions and none seem to work for me. The following are fine:

  • JDK and JRE fully up to date
  • PATH has been set
like image 589
Daniel Kidd Avatar asked Oct 16 '25 03:10

Daniel Kidd


1 Answers

Add the current location to the classpath:

java -cp C:\antlr-3.4.jar;. Test
like image 78
Reimeus Avatar answered Oct 17 '25 18:10

Reimeus