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. ie
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 .bashrc
or CLASSPATH
.
From the command line you need to build ALL the java files together. I am not sure if JavaCC needs javacc.jar
after it's generated your Lexer and Parser. But let's assume it does for some generic AST support.
javacc.jar
is located in ~/javacc-5.0/lib/javacc.jar
root
| Parser.java
| Lexer.java
| Program.java
To compile these I need to run:
javac -cp ~/javacc-5.0/lib/javacc.jar Parser.java Lexer.java Program.java
then I can execute Program
like so if Program has main
java -cp ~/javacc-5.0/lib/javacc.jar:. Program
root
| org
| myproject
| Parser.java
| Lexer.java
| Program.java
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.java
and to execute
java -cp ~/javacc-5.0/lib/javacc.jar:. org.myproject.Program
root
| src
| org
| myproject
| Parser.java
| Lexer.java
| Program.java
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.java
and to execute
java -cp ~/javacc-5.0/lib/javacc.jar:src org.myproject.Program
There is a difference between javac
and java
. You try to use them in wrong way. javac
is a compiler which produces *.class files (in your case Parser.class). This *.class file must be then run by java
.
So if I have file Parser.java
class Parser {
public static void main(String[] args) {
System.out.println("Hi");
}
}
I would go the directory where this files resides and run in shell:
javac Parser.java
Than run the compiled file
java Parser
And that's it. Note that the name of the class in java file must be same as the name of the file.
Create a Jar file of all your compiled classes. Refer to this tutorial on how to create a jar file.
Start you program with this command
java -classpath pathToJarFile com.abc.MainClass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.