Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write "set classpath" in makefile for Java in Linux?

Tags:

java

makefile

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 \

    FileOperation.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...

like image 320
Seen Avatar asked Oct 18 '25 15:10

Seen


1 Answers

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
like image 182
duffymo Avatar answered Oct 21 '25 03:10

duffymo