代码之家  ›  专栏  ›  技术社区  ›  Seen

如何在Linux下用makefile for Java编写“set classpath”?

  •  3
  • Seen  · 技术社区  · 14 年前

    我有一个项目:

    A.java
    B.java
    C.java
    

    A依赖于B.java和C.java,它们应该在同一个文件夹中

    如何将类路径设置为ABC文件的当前文件夹?

    也许这个问题对你来说很简单,但我花了几个小时在谷歌上,仍然无法让它工作。。。

    我的make文件的详细信息是:

    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应该在编译其他java文件之后编译。。。

    我在谷歌上搜索了很多,但不太明白命令make的语法。。。。

    再次原谅我的新手问题。。。

    4 回复  |  直到 14 年前
        1
  •  5
  •   duffymo    14 年前

    如果你有这样的安排(我现在假设没有包裹):

    /src
        A.java
        B.java
        C.java
    

    创建目录 /classes /src . 然后在导航到包含这两个命令的文件夹后,在命令shell中运行此命令 /班级

    javac -d ./classes src/*.java
    

    你可以在 文件夹。

    如果C有需要运行的主方法,您将按如下方式执行:

    java -cp .;classes C
    

    这是我用来做的样品:

    public class A
    {
        public String toString() { return A.class.getName(); }
    }
    

    B、 爪哇语:

    public class B
    {
        public String toString() { return B.class.getName(); }
    }
    

    C、 爪哇语:

    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(); }
    }
    

    如果你坚持使用make,也许这会有帮助:

    http://www.cs.swarthmore.edu/~newhall/unixhelp/javamakefiles.html

    你不是斯沃斯莫尔的学生,是吗?

    #
    # 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
    
        2
  •  0
  •   Adrian Mouat    14 年前

    最好的计划是使用ant(http://ant.apache.org/)而不是make。

    但是如果您想设置类路径,您可以在javac命令(例如javac-cp)中进行设置。A、 或者通过设置classpath环境变量(但我不确定在make中如何做到这一点)。

        3
  •  0
  •   moinudin    14 年前

    确保当前工作目录在类路径中,这意味着目录。必须在类路径中。

    导出classpath变量取决于您运行的是什么。如果是Linux,答案是“export CLASSPATH=$CLASSPATH:.”。

        4
  •  0
  •   kf0l    14 年前

    运行代码的一个快速方法就是用“java C A.java B.javac.java”编译它们 如果您有/需要根权限,则使用“java A.java”或“sudojavaa.java”运行它。

    我还建议使用Eclipse这样的IDE来开发代码。它将为您处理类路径,并通过使用 break points .