Reputation: 445
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.
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. For example, the command lines
make
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.
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...
Could anybody give me some help please?
Thanks
Upvotes: 1
Views: 2309
Reputation: 31290
There is some confusion as to the issues.
First, compile Java using make is a little... iffy. (Most people use ant or maven.) However, if you don't mind a little overhead, you can do it using make. You probably should run make from a directory at the root of the Java package hierarchy. You can determine all Java files below using make macros. Hint: shell:
JAVA_FILES = $(shell find -name \*.java)
Then you run 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 w.r.t. java files. I just use a target "compile", depending on all the java files, and touch a file acting as a dummy target.
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. I'd have to know what "mipsim" is - probably a shell script for doing just that. Anyway, a make target could be the log file:
%.log : %.cmd
${JAVA_HOME}/bin/java -cp ${ROOT} <$< >$@
Now, make test1.cmd
should run your program.
Note: Redirection is not specified by program arguments; this is handled by the shell.
Upvotes: 1
Reputation: 2624
There are a bunch of unrelated questions..
The syntax you are showing:
mipsim -v < test1.cmd > test1.log
would call an executable mipsim. Pass "-v" to the args[1]. Redirect test1.cmd to be the standrad input and test1.log be the standard output. 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
In java these will be redirected to System.in and System.out
About makefiles Basically a make file rule looks like this:
<target>: <dependency1> .. < dependencyn>
~tab~ command
So just like it is possible to build a target that calls 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
If you built a c++ executable then you can execute it from the makefile in the same way.
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 PARA1=1 PARA2=ABC.c
You can use the parameters in your makefile..
test: mipsim
mipsim -v < $(INPUT_FILE) > $(OUTPUT_FILE)
Upvotes: 0
Reputation: 251
Quick comment on your question.
all: build run
build: (this is to build class file from your java source)
run: put your java command line here like "java ..."
When you run "make", it will call "all" target. And all target will call "build" and "run" target, so just put one thing in one target and use the combinations.
Is your java takes input filename as argument or from stdin? If you want to take input filename, then you can take it from args argument passed to your main(String[] args).
If you want to read from stdin then you can create a bufferedreader as below. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Hope this help. (+1 please, if you like this answer)
Upvotes: 0