如何编写“设置类路径”在 Linux 中 Java 的 makefile 中?

发布于 2024-09-30 00:03:10 字数 766 浏览 1 评论 0原文

我有一个关于在 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

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

我用谷歌搜索了很多并且不太理解命令 make 的语法...

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手心的温暖 2024-10-07 00:03:10

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

/src
    A.java
    B.java
    C.java

创建一个与 /src 处于同一级别的目录 /classes。然后导航到包含 /src/classes 的文件夹后,在命令 shell 中运行此命令:

javac -d ./classes src/*.java

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

如果 C 有您需要运行的 main 方法,您将这样做:

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

您不是 Swarthmore 的学生,是吗?

在这里,我根据你的情况修改了他们的制作。更改 .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

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
殤城〤 2024-10-07 00:03:10

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

但如果你想设置类路径,你可以在 javac 命令(例如 javac -cp . A.java)中完成,或者通过设置类路径环境变量(但我不确定你如何在 make 中做到这一点) 。

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

格子衫的從容 2024-10-07 00:03:10

确保当前工作目录位于类路径中,即目录 .必须在类路径中。

导出类路径变量取决于您正在运行的内容。如果是 Linux - 答案是“export CLASSPATH=$CLASSPATH:.”。

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

池予 2024-10-07 00:03:10

运行代码的快速方法就是使用“javac A.java B.java C.java”编译它们
然后使用“java A.java”或“sudo java A.java”运行它(如果您拥有/需要 root 权限)。

我还建议使用 IDE(例如 Eclipse)来开发代码。它将为您处理类路径,并通过使用断点使调试变得更加容易。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文