简体   繁体   English

添加编译器以运行 java 代码并显示 output

[英]Adding a compiler to run a java code and show the output

I am making an android app which can run c, c++ and java programs.我正在制作一个 android 应用程序,它可以运行c、c++ 和 Z93F725A07423FE1C889F448B3程序。 The app stores the respective files in a folder and is made to execute with the following code.该应用程序将各个文件存储在一个文件夹中,并使用以下代码执行。 Whenever I click on compile button it shows an IO Exception saying " error=13 permission denied ".每当我单击编译按钮时,它都会显示一个 IO 异常,说“错误=13 权限被拒绝”。

 try {
                p = Runtime.getRuntime().exec(path + "/PocketIDE/JavaPrograms/"+ filename);
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(p.getInputStream()));
                String line = "";
                while ((line = reader.readLine()) != null) {
                    output2.append(line).append("\n");
                    p.waitFor();
                }
                String response = output2.toString();
                output.setText(response);
            } catch (IOException | InterruptedException e) {
                output.setText(e.toString());
                e.printStackTrace();
            }

Is the above method the correct way to execute the program?上述方法是执行程序的正确方法吗? or do I need to change the code?还是我需要更改代码?

 p = Runtime.getRuntime().exec(path + "/PocketIDE/JavaPrograms/"+ filename);

You shouldn't run arbitrary Java code with the runtime that controls the execution of your app.您不应使用控制应用程序执行的运行时运行任意 Java 代码。 This opens a massive security flaw, so Android disallows it.这会打开一个巨大的安全漏洞,因此 Android 不允许它。 Instead, you should find a way to execute the Java code in its own environment and runtime.相反,您应该找到一种在其自己的环境和运行时中执行 Java 代码的方法。

The statement in your code can be used to execute other programs, but it is not necessarily a good idea.你代码中的语句可以用来执行其他程序,但这不一定是个好主意。

The exec method internally forks the application`s process and creates a new one, which immediately executes the system command you give it. exec方法在内部分叉应用程序的进程并创建一个新进程,该进程会立即执行您给它的系统命令。

From the path in your code I assume that you try to execute a binary executable, which is not allowed anymore by Android since API level 28:从代码中的路径,我假设您尝试执行二进制可执行文件,自 API 级别 28 起,Android 不再允许执行此操作:

Untrusted apps that target Android 10 cannot invoke exec() on files within the app's home directory.以 Android 10 为目标的不受信任的应用程序无法对应用程序主目录中的文件调用 exec()。 This execution of files from the writable app home directory is a W^X violation.从可写应用程序主目录执行文件是 W^X 违规。 Apps should load only the binary code that's embedded within an app's APK file.应用应仅加载嵌入在应用 APK 文件中的二进制代码。

The only possible solution is to reduce the API level to 28 or include the binary in the APK file during packaging.唯一可能的解决方案是将 API 级别降低到 28 或在打包时将二进制文件包含在 APK 文件中。

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

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