0

I'm trying to work on a security system which needs remote debugging.So what I'm searching is a way to execute a code which is in a String,like the example below but with java.

 try {

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

   Runtime.getRuntime().exec(Code);


} catch (IOException e) {
}
polypiel
  • 2,321
  • 1
  • 19
  • 27
  • possible duplicate of [How do I run command line from Java code in the background?](http://stackoverflow.com/questions/11394394/how-do-i-run-command-line-from-java-code-in-the-background) – logoff Mar 28 '14 at 10:58
  • 7
    I would have thought executing any old code in a string is the opposite of a security system... – doctorlove Mar 28 '14 at 10:59

2 Answers2

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());

Please refer the following URL for further information http://www.mkyong.com/java/how-to-execute-shell-command-from-java/

polypiel
  • 2,321
  • 1
  • 19
  • 27
hetptis
  • 786
  • 1
  • 12
  • 23
0

No friend you got it wrong.I really don't want to execute cmd codes.what i really want is to execute java commands.as a string which is passed as shown below.

example :

String code = "System.out.println("Test code")";

Runtime.getRuntime().exec(Code);

something like this.

  • The example in your question states otherwise. I would adapt your original question to tell the truth then. – Gimby Apr 01 '14 at 07:31