简体   繁体   English

如何通过makefile将参数从命令行传递到Java程序

[英]How to pass parameters from command line to a java program through makefile

My first question in stack overflow... Kind of excited but still struggling in the problem. 我在堆栈溢出中的第一个问题...有点激动,但仍在问题中挣扎。

Alright, My question is how to pass parameters from command line to a java program through makefile. 好的,我的问题是如何通过makefile将参数从命令行传递到Java程序。

Honestly I don't really know wether my description is correct....... Cause I don't really know much about makefile... In my assignment, the description is that we must develop a Makefile for GNU make to build our program. 老实说,我真的不知道我的描述是否正确.......因为我对Makefile不太了解...在我的作业中,描述是我们必须为GNU make开发一个Makefile来构建我们的程序。 For example, the command lines 例如,命令行

make 使

mipsim -v < test1.cmd > test1.log mipsim -v <test1.cmd> test1.log

will build the ISS (a simulator we made) and then run it with debugging output, taking input commands from the file test1.cmd and writing result to test1.log. 将构建ISS(我们制造的模拟器),然后以调试输出运行它,并从文件test1.cmd中获取输入命令,并将结果写入test1.log。

I have finished the program but I don't know how to make the things above happen. 我已经完成了该程序,但是我不知道如何使上述事情发生。 What I know so far is just to use makefile to make the .class file from .java file.... I have no idea about how to get test1.cmd as my input file's name and test1.log as my output file's name from command lines.... I guess these two names probably will get into my program through String[] args in the main function... 到目前为止,我所知道的只是使用makefile从.java文件中创建.class文件...。我不知道如何将test1.cmd作为输入文件的名称并将test1.log作为输出文件的名称。命令行...。我猜这两个名称可能会通过主函数中的String [] args进入我的程序...

Could anybody give me some help please? 有人可以给我些帮助吗?

Thanks 谢谢

There is some confusion as to the issues. 关于这些问题有些困惑。

First, compile Java using make is a little... iffy. 首先,使用make编译Java有点困难。 (Most people use ant or maven.) However, if you don't mind a little overhead, you can do it using make. (大多数人使用ant或maven。)但是,如果您不介意过多的开销,则可以使用make来完成。 You probably should run make from a directory at the root of the Java package hierarchy. 您可能应该从Java包层次结构根目录中的目录运行make。 You can determine all Java files below using make macros. 您可以使用make宏确定以下所有Java文件。 Hint: shell: 提示:外壳:

JAVA_FILES = $(shell find -name \*.java)

Then you run javac. 然后运行javac。 (Make sure to define all path names to compilers etc. using make macros.) With Java, it's not easy to derive a make target, because .class files are not 1:1 wrt java files. (确保使用make宏定义到编译器等的所有路径名。)使用Java时,派生make目标并不容易,因为.class文件不是1:1 wrt Java文件。 I just use a target "compile", depending on all the java files, and touch a file acting as a dummy target. 我只是根据所有java文件使用目标“编译”,并触摸充当虚拟目标的文件。

Second, the execution. 第二,执行。 To invoke a Java program that is not in an executable jar, you set the classpath (option -cp), specify the main class name and add command line parameters. 要调用不在可执行jar中的Java程序,请设置类路径(选项-cp),指定主类名称并添加命令行参数。 I'd have to know what "mipsim" is - probably a shell script for doing just that. 我必须知道“ mipsim”是什么-可能就是这样做的shell脚本。 Anyway, a make target could be the log file: 无论如何,make目标可以是日志文件:

%.log : %.cmd
        ${JAVA_HOME}/bin/java -cp ${ROOT} <$< >$@ 

Now, make test1.cmd should run your program. 现在, make test1.cmd应该运行您的程序。

Note: Redirection is not specified by program arguments; 注意:重定向不是由程序参数指定的。 this is handled by the shell. 这是由外壳处理的。

There are a bunch of unrelated questions.. 有很多不相关的问题。

The syntax you are showing: mipsim -v < test1.cmd > test1.log 您显示的语法: mipsim -v < test1.cmd > test1.log

would call an executable mipsim. 会调用可执行的mipsim。 Pass "-v" to the args[1]. 将“ -v”传递给args [1]。 Redirect test1.cmd to be the standrad input and test1.log be the standard output. 将test1.cmd重定向为标准输入,将test1.log重定向为标准输出。 The output input redirection happens by the operating system so in c++ reading from std::cin will read from the file and writing to std::cout will write to test1.log 输出输入重定向由操作系统发生,因此在c ++中,从std :: cin读取将读取文件并写入std :: cout将写入test1.log

In java these will be redirected to System.in and System.out 在Java中,这些将被重定向到System.in和System.out

About makefiles Basically a make file rule looks like this: 关于makefile基本上,make文件规则如下所示:

 <target>: <dependency1> .. < dependencyn>
    ~tab~  command

So just like it is possible to build a target that calls Javac. 因此,就像可以构建一个调用Javac的目标一样。 It is possible to build a target that calls Java.. and so you can build a test target and use it to execute any command you need 可以构建一个调用Java ..的目标,因此您可以构建一个测试目标并使用它执行所需的任何命令

If you built a c++ executable then you can execute it from the makefile in the same way. 如果构建了c ++可执行文件,则可以以相同的方式从makefile执行它。

test: mipsim
      mipsim -v < test1.cmd > test1.log

Your final question about pass parameter values to command line from make file do you mean something like this? 关于将参数值从make文件传递到命令行的最后一个问题是什么意思? make PARA1=1 PARA2=ABC.c 使PARA1 = 1 PARA2 = ABC.c

You can use the parameters in your makefile.. 您可以在makefile中使用参数。

test: mipsim
      mipsim -v < $(INPUT_FILE) > $(OUTPUT_FILE)

Quick comment on your question. 快速评论您的问题。

your makefile needs 2 targets. 您的makefile需要2个目标。 one for build and the other for the run. 一个用于构建,另一个用于运行。

all: build run 全部:构建运行

build: (this is to build class file from your java source) build :(这是从您的java源代码构建类文件)

run: put your java command line here like "java ..." 运行:将您的Java命令行放在此处,例如“ java ...”

When you run "make", it will call "all" target. 当您运行“ make”时,它将调用“全部”目标。 And all target will call "build" and "run" target, so just put one thing in one target and use the combinations. 并且所有目标都将调用“ build”和“ run”目标,因此只需将一件事放在一个目标中并使用组合即可。

Your java code. 您的Java代码。

Is your java takes input filename as argument or from stdin? 您的Java是使用输入文件名作为参数还是来自stdin? If you want to take input filename, then you can take it from args argument passed to your main(String[] args). 如果要获取输入文件名,则可以从传递给main(String [] args)的args参数中获取它。

If you want to read from stdin then you can create a bufferedreader as below. 如果要从stdin读取,则可以创建如下的bufferedreader。 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedReader br =新的BufferedReader(新的InputStreamReader(System.in));

Hope this help. 希望能有所帮助。 (+1 please, if you like this answer) (如果您喜欢此答案,请+1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM