制作 Java Makefile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29184999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a Java Makefile
提问by Dude
EDIT: Essentially, I am trying to do the same thing as typing "javac sim.java" and then "java sim (commands)". Is there a way to replace this using a makefile?
编辑:本质上,我试图做与键入“javac sim.java”然后“java sim(命令)”相同的事情。有没有办法使用makefile替换它?
So I have looked at a lot of examples online and tried to modify them to what I need, but nothing has worked. I need a makefile so that my teacher can create my project just by typing "make" into terminal (this is a requirement of the project for some reason). My end goal is to be able to type "sim" followed by the required commands as defined by my code. The code runs in eclipse, but I can't get it to run using these commands in terminal. It will make the file, but it says "sim: command not found" when I try to type "sim....(arguments)" into terminal. I'm sure this is a dumb question but we have not learned about this in school and I have no experience with Makefile.
所以我在网上看了很多例子,并试图将它们修改为我需要的,但没有任何效果。我需要一个 makefile,这样我的老师就可以通过在终端中输入“make”来创建我的项目(出于某种原因,这是项目的要求)。我的最终目标是能够键入“sim”,后跟我的代码定义的所需命令。代码在 eclipse 中运行,但我无法在终端中使用这些命令运行它。它会生成文件,但是当我尝试在终端中键入“sim ....(arguments)”时,它会显示“sim: command not found”。我敢肯定这是一个愚蠢的问题,但我们在学校还没有了解过这个问题,我也没有使用 Makefile 的经验。
Below is my makefile.
下面是我的makefile。
JFLAGS = -g
JC = javac
OPT = -O3
#OPT = -g
WARN = -Wall
sim: sim.class
sim.class: sim.java
$(JC) $(JFLAGS) sim.java
clean:
$(RM) sim.class
clobber:
$(RM) sim.class
回答by Jeff Miller
For a simple project without many files or dependencies, I simply use scripts.
对于没有很多文件或依赖项的简单项目,我只使用脚本。
To build:
构建:
javac -cp .;* *.java
To run:
跑步:
java -cp .;* SomeMainClass
Replace .
with whatever path(s) you need for your source. The * will use any jar on the default path or use a different path like lib/*
.
替换.
为您的源所需的任何路径。* 将使用默认路径上的任何 jar 或使用不同的路径,如lib/*
.
回答by bmargulies
Using make with java can be an exercise in driving screws with a hammer as soon as you have more than one class.
一旦你有一个以上的类,将 make 与 java 一起使用可以是一种用锤子拧螺丝的练习。
Working java code will use packages. So you'll have a complex directory tree that mirrors your package structure, with your .java source files in it. Writing make rules that 'see' all those files is a pain.
工作 java 代码将使用包。因此,您将拥有一个复杂的目录树,它反映您的包结构,其中包含您的 .java 源文件。编写“查看”所有这些文件的 make 规则很痛苦。
You should invoke javac on all your source files at the same time to get correct results. So, the usual make pattern of 'run one command to turn one source file into one compiled file' does not work so well.
您应该同时对所有源文件调用 javac 以获得正确的结果。因此,通常的“运行一个命令将一个源文件转换为一个编译文件”的 make 模式效果不佳。
However, it seems as if your main problem at the moment is that you expect java to produce an executable; a file with a name like 'sim'. Nope, not going to happen in any simple way. The java compiler produces .class files; a whole tree of them for your source files. You can run directly from them, or you can package them up in a JAR file.
但是,目前您的主要问题似乎是您希望 java 生成可执行文件;一个名为“sim”的文件。不,不会以任何简单的方式发生。java 编译器生成 .class 文件;用于您的源文件的一整棵树。您可以直接从它们运行,也可以将它们打包到 JAR 文件中。
To get all the way to something that appears to be a simple command line executable, you need to use a more complex tool that wraps all this up with a script on the front.
为了得到一个看似简单的命令行可执行文件,您需要使用一个更复杂的工具,在前面用一个脚本包装所有这些。
For now, you just need to do:
现在,您只需要执行以下操作:
java -cp . NAME_OF_THE_CLASS_IN_sim.java
to run after your makefile completes.
在 makefile 完成后运行。
回答by Jamie McLaughlin
make executes a recipe by executing its dependencies, then the recipe itself. Therefore if you add a command which runs your compiled code to the top recipe, typing 'make' will also run the code after it is compiled. I cannot believe how many people do not know this!
make 通过执行其依赖项来执行配方,然后是配方本身。因此,如果您将运行编译代码的命令添加到顶部配方,则键入“make”也将在编译后运行代码。我不敢相信有多少人不知道这一点!
Something like:
就像是:
sim: sim.class
java sim
Will do what your tutor requires.
会做你的导师要求的。