制作Java Makefile [英] Making a Java Makefile

查看:157
本文介绍了制作Java Makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:基本上,我正在尝试输入javac sim.java然后java sim(commands)。有没有办法用makefile替换它?



所以我在网上查看了很多例子并尝试将它们修改为我需要的,但没有任何工作。我需要一个makefile,这样我的老师就可以通过在终端输入make来创建我的项目(由于某种原因,这是项目的要求)。我的最终目标是能够输入sim,然后输入我的代码定义的所需命令。代码在eclipse中运行,但我无法在终端中使用这些命令运行它。它将生成文件,但当我尝试在终端中输入sim ....(arguments)时,它会显示sim:command not found。
我确定这是一个愚蠢的问题,但我们在学校没有学到这一点,而且我没有使用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


解决方案

使用make with java可以很快用锤子驱动螺丝因为你有多个班级。



工作的java代码将使用包。因此,您将拥有一个复杂的目录树,它反映了您的包结构,其中包含.java源文件。编写制作看到所有这些文件的规则很痛苦。



您应该同时在所有源文件上调用javac以获得正确的结果。因此,运行一个命令将一个源文件转换为一个编译文件的常用make模式不能很好地工作。



然而,好像你的主要目前的问题是你希望java生成一个可执行文件;一个名为'sim'的文件。不,不会以任何简单的方式发生。 java编译器生成.class文件;它们的整个树,用于源文件。您可以直接从它们运行,也可以将它们打包到JAR文件中。



为了获得一个似乎是一个简单的命令行可执行文件的东西,你需要使用一个更复杂的工具,用一个脚本包装所有这些前面。



目前,你只需要这样做:

  java -cp。 NAME_OF_THE_CLASS_IN_sim.java 

在makefile完成后运行。


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?

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.

Below is my 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

解决方案

Using make with java can be an exercise in driving screws with a hammer as soon as you have more than one class.

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.

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.

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.

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.

这篇关于制作Java Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆