簡體   English   中英

如何在Linux的makefile中編寫“set classpath”?

[英]How to write “set classpath” in makefile for Java in Linux?

我在Linux中為java編寫makefile有一個新手問題

我有一個項目:

A.java
B.java
C.java

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

應該在編譯其他java文件后編譯Heap.java ...

我google了很多,並不太明白命令的語法....

再次請原諒我的新手問題......

如果您有這樣的安排(我現在假設沒有包裹):

/src
    A.java
    B.java
    C.java

創建與/src相同級別的目錄/classes 然后在導航到包含/src/classes的文件夾后,在命令shell中運行此命令:

javac -d ./classes src/*.java

您將在/classes文件夾中找到所有.class文件。

如果C有你需要運行的主要方法,你會這樣做:

java -cp .;classes C

以下是我以前的樣本:

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

如果你堅持使用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

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

但是如果你想設置類路徑,你可以在javac命令(例如javac -cp.A.java)中或通過設置classpath環境變量(但我不確定如何在make中執行此操作)來執行此操作。

確保當前工作目錄位於類路徑中,這意味着目錄。 必須在類路徑中。

導出classpath變量取決於您正在運行的內容。 如果Linux - 答案是“導出CLASSPATH = $ CLASSPATH:。”。

運行代碼的快速方法是使用'javac A.java B.java C.java'編譯它們然后使用'java A.java'或'sudo java A.java'運行它,如果你有/需要root的話允許。

我還建議使用Eclipse之類的IDE來開發代碼。 它將為您處理類路徑,並通過使用斷點使調試更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM