Is there a java equivalent of the python eval function?

后端 未结 6 1632
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 07:59

Is there a java equivalent of the python eval function?

This would be a function which takes an arbitrary string and attempts to execute it in the current context.

6条回答
  •  忘掉有多难
    2020-11-27 08:38

    It may be that you wish to execute a DOS or unix command in the workflow of Java program execution...

    String command = "cmd /c dir /s";
    String homeDir = "C:\\WINDOWS";
    Process process = Runtime.getRuntime().exec(command + " " + homeDir);
    

    And may be you wish to process the output...

    final InputStream in = process.getInputStream();
    int ch;
    while((ch = in.read()) != -1) {
       System.out.print((char)ch);
    }
    

    Close code with try, catch

提交回复
热议问题