Działa mi takie cuś na Windows 10.
public int run(String command) {
Runtime runTime = Runtime.getRuntime();
try {
Process process = runTime.exec(command);
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufforReader = new BufferedReader(inputStreamReader);
boolean ret = process.waitFor(5,TimeUnit.SECONDS);
System.out.println("Runinng return: "+ret);
String line = null;
while((line = bufforReader.readLine()) != null) {
System.out.println(">>> "+line);
}
if (ret == true)
return 0;
else
return 1;
} catch (IOException ex) {
Logger.getLogger(WinRegTest.class.getName()).log(Level.SEVERE, null, ex);
return -1;
} catch (InterruptedException ex) {
Logger.getLogger(WinRegTest.class.getName()).log(Level.SEVERE, null, ex);
return -2;
}
}
Jako komendę daję shella, który uruchamia innego shella w ukrytym okienku z prawami administratora, czyli takie cuś
powershell Start-Process powershell -windowstyle hidden -verb runas -ArgumentList 'New-Item -Path HKCU:\\Software -Name DUPA -Force'
No a to taki mały skrót skrótu
public int runPowerShell(String command) {
return run("powershell Start-Process powershell -windowstyle hidden -verb runas -ArgumentList '"+command+"'");
}
W miejsce "command" można już używać faktycznie poleceń shella
Mam nadzieję, że się to komuś przyda.
Pozdrawiam