如何在Linux的makefile中编写"set classpath"?

See*_*een 3 java makefile

我在Linux中为java编写makefile有一个新手问题

我有一个项目:

A.java
B.java
C.java
Run Code Online (Sandbox Code Playgroud)

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

假设当我进入文件夹时,我可以运行make命令来生成类.

如何将类路径设置为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
Run Code Online (Sandbox Code Playgroud)

应该在编译其他java文件后编译Heap.java ...

我google了很多,并不太明白命令的语法....

再次请原谅我的新手问题......

duf*_*ymo 5

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

/src
    A.java
    B.java
    C.java
Run Code Online (Sandbox Code Playgroud)

创建/classes与...相同级别的目录/src.然后导航到包含两个文件夹后,运行在命令shell命令/src/classes:

javac -d ./classes src/*.java
Run Code Online (Sandbox Code Playgroud)

您将在/classes文件夹中找到所有.class文件.

如果C有你需要运行的主要方法,你会这样做:

java -cp .;classes C
Run Code Online (Sandbox Code Playgroud)

以下是我以前的样本:

A.java:

public class A
{
    public String toString() { return A.class.getName(); }
}
Run Code Online (Sandbox Code Playgroud)

B.java:

public class B
{
    public String toString() { return B.class.getName(); }
}
Run Code Online (Sandbox Code Playgroud)

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(); }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

在这里,我已经为你的案子篡改了他们的作品.更改.java文件,看看它是否有效.

#
# 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
Run Code Online (Sandbox Code Playgroud)

  • 没有人写Java使用make.*没有人*.它可以是IDE或Ant.如果您坚持使用make,请使用-cp参数将其放在java命令中. (2认同)