如何编写“设置类路径”在 Linux 中 Java 的 makefile 中?
我有一个关于在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有这样的安排(我假设现在没有包):
创建一个与
/src
处于同一级别的目录/classes
。然后导航到包含/src
和/classes
的文件夹后,在命令 shell 中运行此命令:您将在
中找到所有 .class 文件/classes
文件夹。如果 C 有您需要运行的 main 方法,您将这样做:
以下是我用来执行此操作的示例:
A.java:
B.java:
C.java:
如果您坚持使用 make,也许这将有所帮助:
http://www.cs.swarthmore.edu/~newhall /unixhelp/javamakefiles.html
您不是 Swarthmore 的学生,是吗?
在这里,我根据你的情况修改了他们的制作。更改 .java 文件并查看它是否有效。
If you have an arrangement like this (I'll assume no packages for now):
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
: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:
Here are the samples that I used to do it:
A.java:
B.java:
C.java:
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.
最好的计划是使用 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).
确保当前工作目录位于类路径中,即目录 .必须在类路径中。
导出类路径变量取决于您正在运行的内容。如果是 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:.".
运行代码的快速方法就是使用“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.