简体   繁体   English

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

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

I have a newbie question for writing makefile for java in Linux 我在Linux中为java编写makefile有一个新手问题

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 A依赖于B.java和C.java,它们应该在同一个文件夹中

It is supposed that when I entered the folder, I can run the make command to generate classes. 假设当我进入文件夹时,我可以运行make命令来生成类。

How can I set the classpath as the current folder of the ABC file? 如何将类路径设置为ABC文件的当前文件夹?

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: 我的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 should be compiled after the other java files are complied... 应该在编译其他java文件后编译Heap.java ...

I googled a lot and does not quite understand the grammar for the command make.... 我google了很多,并不太明白命令的语法....

Excused me again for my newbie problem... 再次请原谅我的新手问题......

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 . 创建与/src相同级别的目录/classes Then run this command in a command shell after navigating to the folder that contains both /src and /classes : 然后在导航到包含/src/classes的文件夹后,在命令shell中运行此命令:

javac -d ./classes src/*.java

You'll find all your .class files in the /classes folder. 您将在/classes文件夹中找到所有.class文件。

If C has the main method you need to run, you'll do it like this: 如果C有你需要运行的主要方法,你会这样做:

java -cp .;classes C

Here are the samples that I used to do it: 以下是我以前的样本:

A.java: A.java:

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

B.java: B.java:

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

C.java: 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: 如果你坚持使用make,也许这会有所帮助:

http://www.cs.swarthmore.edu/~newhall/unixhelp/javamakefiles.html 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. 更改.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

The best plan would be to use ant (http://ant.apache.org/) rather than make. 最好的计划是使用ant(http://ant.apache.org/)而不是make。

But if you want to set the classpath, you can either do it in the javac command (eg javac -cp . A.java) or by setting the classpath environment variable (but I'm not sure how you would do that within make). 但是如果你想设置类路径,你可以在javac命令(例如javac -cp.A.java)中或通过设置classpath环境变量(但我不确定如何在make中执行此操作)来执行此操作。

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. 导出classpath变量取决于您正在运行的内容。 If Linux - the answer is to "export CLASSPATH=$CLASSPATH:.". 如果Linux - 答案是“导出CLASSPATH = $ CLASSPATH:。”。

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. 运行代码的快速方法是使用'javac A.java B.java C.java'编译它们然后使用'java A.java'或'sudo java A.java'运行它,如果你有/需要root的话允许。

I also suggest using an IDE such as Eclipse to develop your code. 我还建议使用Eclipse之类的IDE来开发代码。 It will handle the classpath for you and make debugging much easier by using break points . 它将为您处理类路径,并通过使用断点使调试更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM