Make a system call in dart?

2019-04-07 22:32发布

问题:

I want to execute a python or a java class from inside dart.

The following is a snippet which I have used from a stackoverflow question Java

Runtime currentRuntime = Runtime.getRuntime();
Process executeProcess = currentRuntime.exec("cmd /c c:\\somepath\\pythonprogram.py");

I would like to know how to make such calls in dart.

Basically I have a UI where in the user uploads code in java and python.I want to execute the uploaded code from the dart environment instead of creating a routine in java or python on the folder where the code is uploaded.

I personally dont know if this is possible, since dart is purely in a VM.

I want to execute the following command

java abc

from inside dart.

回答1:

You can simply use Process.run.

import 'dart:io';

main() {
  Process.run('java', ['abd']);
}

You can also access to stdout, stderr and exitCode through the resulting ProcessResult :

import 'dart:io';

main() {
  Process.run('java', ['abd']).then((ProcessResult pr){
    print(pr.exitCode);
    print(pr.stdout);
    print(pr.stderr);
  });
}


标签: dart