How to write "set classpath" in makefile for Java in Linux?
I have a newbie question for writing makefile for java in Linux
I have a project with:
A.java
B.java
C.java
A is dependent on B.java and C.java, they should be in the same folder
It is supposed that when I entered the folder, I can run the make command to generate classes.
How can I set the classpath as the current folder of the A B C file?
Maybe this question would be easy to you but I spend hours to google and still cannot make it work...
Thanks again.
The details of my make file is:
JFLAGS = -g
JC = javac
CLASSPATH = .
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
Heap.class: FibonacciHeap.java \
FileOperat开发者_如何学JAVAion.java \
MinLeftistTree.java \
RandomPermutation.java \
Heap.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
Heap.java should be compiled after the other java files are complied...
I googled a lot and does not quite understand the grammar for the command make....
Excused me again for my newbie problem...
If you have an arrangement like this (I'll assume no packages for now):
/src
A.java
B.java
C.java
Create a directory /classes
at the same level as /src
. Then run this command in a command shell after navigating to the folder that contains both /src
and /classes
:
javac -d ./classes src/*.java
You'll find all your .class files in the /classes
folder.
If C has the main method you need to run, you'll do it like this:
java -cp .;classes C
Here are the samples that I used to do it:
A.java:
public class A
{
public String toString() { return A.class.getName(); }
}
B.java:
public class B
{
public String toString() { return B.class.getName(); }
}
C.java:
public class C
{
public static void main(String [] args)
{
A a = new A();
B b = new B();
C c = new C();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
public String toString() { return C.class.getName(); }
}
If you insist on using make, perhaps this will help:
http://www.cs.swarthmore.edu/~newhall/unixhelp/javamakefiles.html
You aren't a Swarthmore student, are you?
Here, I've doctored their make for your case. Change the .java files and see if it works.
#
# define compiler and compiler flag variables
#
JFLAGS = -g -cp .:./classes -d ./classes
JC = javac
#
# Clear any default targets for building .class files from .java files; we
# will provide our own target entry to do this in this makefile.
# make has a set of default targets for different suffixes (like .c.o)
# Currently, clearing the default for .java.class is not necessary since
# make does not have a definition for this target, but later versions of
# make may, so it doesn't hurt to make sure that we clear any default
# definitions for these
#
.SUFFIXES: .java .class
#
# Here is our target entry for creating .class files from .java files
# This is a target entry that uses the suffix rule syntax:
# DSTS:
# rule
# 'TS' is the suffix of the target file, 'DS' is the suffix of the dependency
# file, and 'rule' is the rule for building a target
# '$*' is a built-in macro that gets the basename of the current target
# Remember that there must be a < tab > before the command line ('rule')
#
.java.class:
$(JC) $(JFLAGS) $*.java
#
# CLASSES is a macro consisting of 4 words (one for each java source file)
#
CLASSES = \
Foo.java \
Blah.java \
Library.java \
Main.java
#
# the default make target entry
#
default: classes
#
# This target entry uses Suffix Replacement within a macro:
# $(name:string1=string2)
# In the words in the macro named 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .java of all words in the macro CLASSES
# with the .class suffix
#
classes: $(CLASSES:.java=.class)
#
# RM is a predefined macro in make (RM = rm -f)
#
clean:
$(RM) *.class
The best plan would be to use ant (http://ant.apache.org/) rather than make.
But if you want to set the classpath, you can either do it in the javac command (e.g. javac -cp . A.java) or by setting the classpath environment variable (but I'm not sure how you would do that within make).
Make sure the current working directory is in the classpath, which means the directory . must be in the classpath.
Exporting the classpath variable depends on what you're running on. If Linux - the answer is to "export CLASSPATH=$CLASSPATH:.".
A quick way to run your code would just be compile them with 'javac A.java B.java C.java' and then run it with 'java A.java' or 'sudo java A.java' if you have/need root permission.
I also suggest using an IDE such as Eclipse to develop your code. It will handle the classpath for you and make debugging much easier by using break points.
精彩评论