我正在开发一个项目,我必须将C / C ++应用程序移植到Android。我使用CMake和https://github.com/taka-no-me/android-cmake
中的android工具链进行了成功构建在构建之后,我将可执行文件推送到Android模拟器并在shell(ADB shell)中调用它们。这些东西运作良好。目前我正在开发一个Android应用程序,我想从Java调用复制的可执行文件。 这些文件位于/ data / local / myfolder下,并具有以下权限
-rwxrwxrwx root root 37 2016-08-02 06:30 rexx
我的问题是,是否可以从Java代码运行我的可执行文件?我尝试了以下来源:
private void runSingleCommand(){
try {
Process process = Runtime.getRuntime().exec("rexx", null, new File("/data/local/myfolder/")); //Permission denied exception
InputStreamReader reader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(reader);
int numRead;
char[] buffer = new char[5000];
StringBuffer commandOutput = new StringBuffer();
while ((numRead = bufferedReader.read(buffer)) > 0) {
commandOutput.append(buffer, 0, numRead);
}
bufferedReader.close();
process.waitFor();
System.out.println("--> Output: "+commandOutput.toString());
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
执行此代码我得到以下异常
java.lang.RuntimeException: java.io.IOException: Error running exec(). Command: [rexx.sh] Working Directory: /data/local/oorexx Environment: null
at org.oorexx.oorexxforandroid.org.oorexx.oorexxforandroid.exec.RexxExecuter.runSingleCommand(RexxExecuter.java:146)
at org.oorexx.oorexxforandroid.org.oorexx.oorexxforandroid.exec.RexxExecuter.runTheRexxImage(RexxExecuter.java:24)
at org.oorexx.oorexxforandroid.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.IOException: Error running exec(). Command: [rexx.sh] Working Directory: /data/local/oorexx Environment: null
at java.lang.ProcessManager.exec(ProcessManager.java:211)
at java.lang.Runtime.exec(Runtime.java:173)
at java.lang.Runtime.exec(Runtime.java:246)
at org.oorexx.oorexxforandroid.org.oorexx.oorexxforandroid.exec.RexxExecuter.runSingleCommand(RexxExecuter.java:127)
at org.oorexx.oorexxforandroid.org.oorexx.oorexxforandroid.exec.RexxExecuter.runTheRexxImage(RexxExecuter.java:24)
at org.oorexx.oorexxforandroid.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.IOException: Permission denied
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:209)
at java.lang.Runtime.exec(Runtime.java:173)
at java.lang.Runtime.exec(Runtime.java:246)
at org.oorexx.oorexxforandroid.org.oorexx.oorexxforandroid.exec.RexxExecuter.runSingleCommand(RexxExecuter.java:127)
at org.oorexx.oorexxforandroid.org.oorexx.oorexxforandroid.exec.RexxExecuter.runTheRexxImage(RexxExecuter.java:24)
at org.oorexx.oorexxforandroid.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
我经常搜索,但是如果(以及如何)在没有root用户的情况下从Java运行可执行文件,我没有找到一个明确的答案。