在Linux中如何在Makefile中为Java写“ set classpath”?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Linux中如何在Makefile中为Java写“ set classpath”?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3299字,纯文字阅读大概需要5分钟。
内容图文
我在Linux中为Java编写makefile有一个新手问题
我有一个项目:
A.java
B.java
C.java
A依赖于B.java和C.java,它们应位于同一文件夹中
假定当我进入文件夹时,我可以运行make命令来生成类.
如何将类路径设置为A B C文件的当前文件夹?
也许这个问题对您来说很容易,但我花了几个小时去Google上搜索,但仍然无法正常工作…
再次感谢.
我的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文件之后进行编译…
我在Google上搜索了很多,对命令make的语法不太了解.
再次因为我的新手问题而责备我…
解决方法:
如果您有这样的安排(我现在假设没有软件包):
/src
A.java
B.java
C.java
在与/ src相同的级别上创建目录/ classes.浏览到同时包含/ src和/ classes的文件夹后,在命令外壳中运行此命令:
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
您不是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
内容总结
以上是互联网集市为您收集整理的在Linux中如何在Makefile中为Java写“ set classpath”?全部内容,希望文章能够帮你解决在Linux中如何在Makefile中为Java写“ set classpath”?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。