tags:

views:

149

answers:

4

Hi guys,

i would like to ask if it is possible to run a make file in java?

I was searching the internet but only got the examples of generating java make file. My case is different, what if i have makefile, but i just want to run it from java?

really sorry for dumb question, but i am lost and dont even know if i can run it or not.

thank you very much!

+1  A: 

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 execute make from within your java app. You can do this via Runtime.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=1

One 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.

echo
And another issue is that makefiles tend to be non-portable.
Stephen C
@Stephen C - Good point.
echo
oh thanks, sorry for not replying for a long time. @echo thanks for the advice, i just ran the make utility for the makefile, not from java. thanks for replies!
Bugzy bug
Doesn't using Runtime.exec() entail a lot of performance overhead? I realize that, at times, there may be no alternative.
Epitaph
A: 

i was running from the command line coz got confused with smarter ways of doing it =(

Bugzy bug
What makes a command line not smart?
mikerobi
A: 

I have another question. If i don't find the make utility or probably I don't have it on my machine, what do I do?

Where do I find it to download or something?

mistique
If you are on unix, you can probably do a "sudo apt-get install build-essentials" via Runtime.exec
Epitaph
@Epitaph: That's going to depend on the flavor of Unix/Linux.
GreenMatt
@GreenMatt Yes. I probably should have specifically mentioned Ubuntu in this case.
Epitaph
A: 

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.

Epitaph