As you may already know, Java organizes a program into classes, with each class
in a program compiled into a separate binary object code file (.class files).
It is good practice to separate the source code (in .java files) from the Java classfiles. A good way to do it is to create in your compilation directory a sources directory and a classes directory.
The directory in which source files (.java files) should be stored is compilation/sources/filter.
The directory into which class files (.class files) are compiled is compilation/classes.
javac is the Java compiler that you will use during the project.
DESTINATION
By default, javac places the class files it generates in the same directory as the corresponding source file. To override this behavior, use the -d option.
The -d directory option will make the specified directory the root of the class hierarchy. All the .class files are then placed in this directory, or in the appropriate subdirectory below it, depending on the package name of the class.
Example:
javac -d ~/compilation/classes ~/compilation/sources/filter/Toto.java
creates a classfile for the sourcefile Toto.java in the directory compilation/classes.
When the -d option is specified, javac automatically creates any directories it needs to store its class files in the appropriate place.
When you are in the right directory, you do not need to give the full
path of the sourcefile.
Example:
cd ~/compilation/sources/filter
javac -d ~/compilation/classes Toto.java
CLASSPATH
When a source file references a class, javac searches for the definition of that class using the class path. To specify the path javac uses to look up classes, use the -classpath path option.
Example:
javac -classpath ~/compilation/classes -d ~/compilation/classes \
~/compilation/sources/filter/Titi.java
creates a classfile Titi.class somewhere in the directory compilation/classes and looks up classes referenced in Titi.java in the directory compilation/classes.
If you are too lazy to write the -classpath option all the time, you can also
set the environment variable CLASSPATH to your classes directory. For
tcsh this would look like this:
setenv CLASSPATH ~/compilation/classes
For sh or bash:
CLASSPATH=~/compilation/classes; export CLASSPATH
If you want to put more than one directory in your classpath, you can
separate them by :
JAVA INTERPRETER
The java command executes Java programs.
The path that java uses to look up the specified class and all other classes that it loads can be set by using the -classpath path option
(if the classpath was not set already by the CLASSPATH variable).
Example:
java -classpath ~/compilation/classes filter.Titi
runs the main() method of the Titi class. The interpreter looks for all the classes in the directory compilation/classes.
JLEX
JLex is itself a java program. First you need to install it by putting
JLex.jar in ~/compilation/classes
Then run JLex from the interpreter:
Example:
java -classpath ~/compilation/classes/JLex.jar JLex.Main \
~/compilation/sources/filter/filter.lex
mv ~/compilation/sources/filter/filter.lex.java \
~/compilation/sources/filter/Scanner.java
These two comands leave a generated scanner in Scanner.java .
Probably you want to run these commands from the right directory:
Example:
cd ~/compilation/sources/filter
java -classpath ~/compilation/classes/JLex.jar JLex.Main filter.lex
mv filter.lex.java Scanner.java
MAKE
Instead of typing these long commands repeatedly you might want to use
Makefiles.
You can put the following lines in a file
~/compilation/sources/filter/Makefile :
scanner:
<tab>java -classpath $(HOME)/compilation/classes/JLex.jar JLex.Main \
$(HOME)/compilation/sources/filter/filter.lex
<tab>mv $(HOME)/compilation/sources/filter/filter.lex.java \
$(HOME)/compilation/sources/filter/Scanner.java
Example:
cd ~/compilation/sources/filter
make scanner
now calls the two commands for you. You can have multiple paragraphs
like the one above in a Makefile. Be sure really to use tabulators and
not spaces to indent in a Makefile.
JAVACUP
As JLex, java_cup is itself a java program. First you need to install it by putting
java_cup.jar in ~/compilation/classes
Then run java_cup from the interpreter:
Example:
java -classpath ~/compilation/classes/java_cup.jar java_cup.Main \
-package filter -parser Parser -symbols Tokens \
-interface ~/compilation/sources/filter/filter.cup
This generates the parser in Parser.java and the token
numbers in Tokens.java
DEBUGGER
If you are used to debug programs with a source-level debugger, then you
should try out jdbg. It is
used like java but you also have to provide the path to your sources, so that
the debugger can find your sourcefiles.
Example:
~czenger/jdbg/bin/jdbg -classpath ~/compilation/classes \
-sourcepath ~/compilation/sources filter.Titi
runs the main() method of the Titi class. The interpreter looks for all the classes in the directory compilation/classes.
|