如何在Java中从字符串执行代码

如何在Java中从字符串执行代码,java,Java,我正在尝试一个需要远程调试的安全系统,所以我正在搜索一种执行字符串代码的方法,就像下面的例子,但是使用java try { String Code = "rundll32 powrprof.dll, SetSuspendState";// the code we need to excecute Runtime.getRuntime().exec(Code); } catch (IOException e) { } 请参考以下URL以了解更多信息没有朋友您弄错了。我真的不

我正在尝试一个需要远程调试的安全系统,所以我正在搜索一种执行字符串代码的方法,就像下面的例子,但是使用java

 try {

   String Code = "rundll32 powrprof.dll, SetSuspendState";// the code we need to excecute

   Runtime.getRuntime().exec(Code);


} catch (IOException e) {
}

请参考以下URL以了解更多信息

没有朋友您弄错了。我真的不想执行cmd代码。我真正想要的是执行java命令。作为字符串传递,如下所示

例如:

String code=“System.out.println(“测试代码”)”

Runtime.getRuntime().exec(代码)


类似这样的情况。

可能重复我认为在字符串中执行任何旧代码都是安全系统的对立面…您问题中的示例说明了其他情况。我会修改你原来的问题,然后说实话。
    String Code = "rundll32 powrprof.dll, SetSuspendState";

    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(Code);
        p.waitFor();
        BufferedReader reader = 
                        new BufferedReader(new InputStreamReader(p.getInputStream()));

                    String line = "";           
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println(output.toString());