I have a project due soon and everything is coming together really nicely, but java classpaths are getting seriously in the way. I’ll try to explain the situation as clearly and thoroughly as I can.
So we are using Javacc to write a programming language. The javacc file compiles into several java files. The Parser.java file includes references and calls to the other generated java files. So, after generation, we compile the Parser.java file. The problem is we get many errors, including not being able to recognize the calls to the other java files as well as our own files. We asked on a classroom discussion board about our problem and the professor responded with “you need to have the class files in your classpath”. Ok, great, so the question is, how do we do that? Basically we have a single directory with the generated java files and our other helper files.
So, what have we tried?
I have tried changing my .bashrc (Ubuntu) file to include the correct classpath but that doesn’t work. i.e.
CLASSPATH=Home/project
(something like that I had the syntax right in the file)
I’ve tried on compilation executing
javac -cp . Parser.java
and
javac -cp "." Parser.java
neither works.
I have tried to edit the xml (I think xml) .classpath file in the directory of our files. Still doesn’t work.
Somehow, I was able to compile Parser.java in one of the directories I have (we ended up making multiple directories with the same files in it in a futile effort to make something work) but when I try to run
java -cp . Parser.java
or
java Parser.java
It says it can not find the main and throws (I believe, its on my other computer) ClassNotFound or ClassNotDefined exception (something like that, it cannot find the main in the Parser file eventhough it IS there).
We have tried adding packages deceleration and import statements to our file, nothing seems to work.
BASICALLY: How can I successfully change the Classpath so that my java files (all in one directory and not jarred) can be compiled and run on my machine?
Thank you for your help. I greatly appreciate it.
I highly suggest you look into Ant. A simple build file can solve all these problems for you.
You don’t need to edit your
.bashrcorCLASSPATH.From the command line you need to build ALL the java files together. I am not sure if JavaCC needs
javacc.jarafter it’s generated your Lexer and Parser. But let’s assume it does for some generic AST support.javacc.jaris located in~/javacc-5.0/lib/javacc.jarScenario 1: Simple directory structure, all Java files are in the root folder with no package.
To compile these I need to run:
javac -cp ~/javacc-5.0/lib/javacc.jar Parser.java Lexer.java Program.javathen I can execute
Programlike so if Program hasmainjava -cp ~/javacc-5.0/lib/javacc.jar:. ProgramScenario 2: Medium directory structure, code in root but with packages.
then you need to execute javac like so:
javac -cp ~/javacc-5.0/lib/javacc.jar org/myproject/Parser.java org/myproject/Lexer.java org/myproject/Program.javaand to execute
java -cp ~/javacc-5.0/lib/javacc.jar:. org.myproject.ProgramScenario 3: Complex directory structure, specific source directory with packages.
then you need to execute javac like so:
javac -cp ~/javacc-5.0/lib/javacc.jar -sourcepath src src/org/myproject/Parser.java src/org/myproject/Lexer.java src/org/myproject/Program.javaand to execute
java -cp ~/javacc-5.0/lib/javacc.jar:src org.myproject.Program