gpt4 book ai didi

java - 如何在 Linux 的 Java 生成文件中写入 "set classpath"?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:45 27 4
gpt4 key购买 nike

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

我有一个项目:

A.java
B.java
C.java

A依赖B.java和C.java,应该在同一个文件夹下

应该是进入文件夹后可以运行make命令生成类

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

也许这个问题对你来说很容易,但我花了几个小时去谷歌,但仍然无法解决...

再次感谢。

我的 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... 的语法。

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

最佳答案

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

/src
A.java
B.java
C.java

/src 的同一层创建目录/classes。然后在导航到包含 /src/classes 的文件夹后在命令 shell 中运行此命令:

javac -d ./classes src/*.java

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

如果 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

你不是斯沃斯莫尔学院的学生,对吧?

在这里,我已经为您的情况篡改了他们的作品。更改 .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

关于java - 如何在 Linux 的 Java 生成文件中写入 "set classpath"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4063863/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com