Clase de Java que ejecuta PowerShell (la clase tiene que ser compilada)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.io.IOException; import java.util.concurrent.TimeUnit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class EjecutarPowerShell { public static void main(String[] args) throws InterruptedException { Runtime runtime = Runtime.getRuntime(); try { Process process = runtime.exec("powershell.exe $PSVersionTable.PSVersion"); process.getOutputStream().close(); String line; BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((line = stdout.readLine()) != null){ System.out.println(line); } TimeUnit.SECONDS.sleep(10); } catch(IOException ex){ System.err.println("Error"); System.exit(-1); } } } |
Clase de Java que compila y ejecuta una clase de Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.io.IOException; import java.util.concurrent.TimeUnit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class EjecutarJava { public static void main(String[] args) throws InterruptedException { Runtime runtime = Runtime.getRuntime(); try { Process process = runtime.exec("javac EjecutarPowerShell.java"); process.getOutputStream().close(); String line; BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((line = stdout.readLine()) != null){ System.out.println(line); } Process process2 = runtime.exec("java EjecutarPowerShell"); process2.getOutputStream().close(); String line2; BufferedReader stdout2 = new BufferedReader(new InputStreamReader(process2.getInputStream())); while ((line2 = stdout2.readLine()) != null){ System.out.println(line2); } } catch(IOException ex){ System.err.println("Error"); System.exit(-1); } } } |