是否可以从 Java 运行 Makefile?
我在互联网上搜索,但只找到了生成 Java Makefile 的示例。我的情况有所不同,如果我有一个 Makefile,但我只想从 Java 运行它怎么办?
I was searching the Internet but only got examples of generating a Java Makefile. My case is different, what if I have a Makefile, but I just want to run it from Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您并不是真正“运行”makefile,而是运行
make
实用程序,它会在当前目录中查找名为“makefile”或“Makefile”的文件并处理该文件中的命令。因此,您需要在 Java 应用程序中执行make
。您可以通过 Runtime.exec() 来完成此操作。有关详细信息,请参阅 http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29 和 http://www.javaworld.com/javaworld/ jw-12-2000/jw-1229-traps.html?page=1您应该注意的一个潜在问题是
make
可能存在于您的程序所在的计算机上,也可能不存在继续运行,即使它确实存在,它也可能位于非标准位置。在我的机器上,它是/usr/bin/make
,但它可能位于其他地方。请小心考虑这一点。You don't really "run" a makefile, rather, you run the
make
utility and it looks for a file called "makefile" or "Makefile" in your current directory and processes the commands in that file. So you need to executemake
from within your java app. You can do this viaRuntime.exec()
. For more information see http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29 and http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1One potential issue you should look out for is that
make
may or may not exist on the machine that your program runs on, and even if it does exist, it may be located in a non-standard location. On my machine, it's/usr/bin/make
, but it could be located elsewhere. Be careful to account for this.是的。您可以使用 Runtime.exec 来运行 make 应用程序。但是,正如前面有人所说,您应该考虑文件可能不存在或存在于其他位置的情况。我建议您查看 java.io.File javadoc 它提供了此功能。另外,不要忘记捕获错误流以用于日志记录/调试目的。很多时候,程序员只实现输入和输出流。
Yes. You can use Runtime.exec to run the make application. However, as someone previously stated, you should account for conditions when the file may not be present or present in another location. I suggest you to have a look at java.io.File javadoc which provides this functionality. Also, don't forget to capture the error stream for logging/debugging purposes. Many a times, programmers only implement the input and output streams.