【问题标题】:How to execute code from a String in Java如何在 Java 中从字符串执行代码
【发布时间】:2014-03-28 10:56:25
【问题描述】:

我正在尝试在需要远程调试的安全系统上工作。所以我正在搜索的是一种执行字符串中的代码的方法,如下例所示,但使用 java。

 try {

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

   Runtime.getRuntime().exec(Code);


} catch (IOException e) {
}

【问题讨论】:

标签: java


【解决方案1】:
    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());

更多信息请参考以下网址http://www.mkyong.com/java/how-to-execute-shell-command-from-java/

【讨论】:

    【解决方案2】:

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

    例子:

    String code = "System.out.println("测试代码")";

    Runtime.getRuntime().exec(代码);

    类似的东西。

    【讨论】:

    • 您问题中的示例另有说明。我会修改你原来的问题,然后说实话。
    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 2012-07-26
    • 1970-01-01
    • 2011-07-27
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多