tags:

views:

58

answers:

4

Hello, 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...

A: 

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

Adrian Mouat
We're already gone over that. make is required.
duffymo
A: 

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:.".

marcog
Could you take a look at my makefile and make some suggestions? Thank you very much...
Seen
A: 

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.

The problem is that Seen's code compiles and runs just fine in Eclipse, but s/he can't compile using make. The command shell is a mystery.
duffymo
+2  A: 

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
duffymo
Thanks very much for your prompt reply. This is working if I type in my answer in the command lines...But it is a little bit difficult for me to put in the make file..I am not familiar with the compiler of "make" in Java and their documents are hard to understand...
Seen
Nobody who writes Java uses make. *Nobody*. It's either an IDE or Ant. If you insist on using make, put it in the java command using the -cp argument.
duffymo
I've found a make file and doctored it to show where the classpath argument goes. See if that sorts you out.
duffymo
That is how I began...Unfortunately, I am exhausted to make it work. I spent at least 5 hours to write a make function and tried many possibilities. The sad thing for me is that if I don't finish this, I would not get credit for my course (Although I spend at least 50 hours to write all the functions and make them working in Eclipse).
Seen
If you don't mind....Could I sent you the files I am working on for you to take a look? If you do can help me I would like to buy you a beer :).
Seen
duffymo
I have given you the .java files; I've given you the make. I've already done enough. All you have to do is try it. No beer for me, thank you. Just vote my efforts up and try it.
duffymo
Don't think about sending me anything. Try it or take your failing grade now.
duffymo
Thank you..I finally figure it out...
Seen
"You" figured it out? Or did the make file that you were given finally work?
duffymo