Skip to main content

How Can I execute cmd commands with RunTime??

1 reply [Last post]
Ryuuji
Offline
Joined: 2012-11-19
Points: 0

Hello I've got a question How can execute cmd commands as admini.

I need to create Mysql backups using runtime but to do that I need to execute cmd as admin, I've got this code:

public static void main(String[] args) {
Runtime c = Runtime.getRuntime();
String cmd = "cmd /c cd C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin ";
String resp ="cmd /c mysqldump -u root -proot sakila > C:\\pr\\javaMike.sql";

try {
c.exec(cmd);
c.exec(resp);
System.out.println(System.getProperty("os.name"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

with this I can make the backup file but save it without information, I mean 0kb and if I execute directly as admin in cmd, cand do it without problems, some solution for me?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bparker1084
Offline
Joined: 2012-12-04
Points: 0

If you want to run several commands in the cmd shell then you can construct a single command like this: rt.exec("cmd /c start cmd.exe /K \"cd c:/ && dir\"");