Running a Unix Executable Objective-C file in Java (OS X)
I've seen this question asked before and tried following the solutions posted. Unfortunately, they aren't working. I'm trying to make a simple Java program that can run a compiled Objective-C program (Unix Executable file) with an input para开发者_如何转开发meter. Below are the attempts at Java I have tried that don't seem to be working:
String[] cmd = {"/bin/bash", fullFilePath, Param};
Runtime.getRuntime().exec(cmd);
This is generating a Processor error of 126 for "Command invoked cannot execute". I've tried other variations that don't work as well such as:
String[] cmd = {"/bin/bash fullFilePath \"Param\""};
String[] cmd = {"/usr/bin/open fullFilePath \"Param\""};
Any suggestions or ideas on how I can get this to work? I just need to run the compiled Objective-C program in Java with the parameter. I figured it wouldn't be this hard. Thanks in advance, and if you need more information just ask.
Is there any particular reason you're executing your executable through the command line? Being more of a Windows person I might be missing something, but surely just the following should work:
String[] cmd = {fullFilePath, Param};
Runtime.getRuntime().exec(cmd);
(assuming fullFilePath
is indeed the full absolute path to your executable).
BTW: the fact that the other program is written in Objective-C is almost certainly irrelevant.
Try using sh
:-
String[] cmd = {"/bin/sh", "-c", fullFilePath, Param};
Runtime.getRuntime().exec(cmd);
The reason for error 126 is "Permission problem or command is not an executable".
Make sure that your file is executable from the command line first. You may have to set the execute bit first using chmod.
chmod a+x <filename>
精彩评论