3

I want to execute the following mysql query, using java code:

mysqldump -uroot -ppassword temp1>C:\abc.sql

After a lot of search I found out that this needs cmd to run as admin I don't know how to run as admin using java code. I tried this but it doesn't work

runtimeProcess = Runtime.getRuntime().exec(new String[] { "runas /profile /user:Administrator \"cmd.exe", executeCmd });

Does any one has any idea regarding this. Thanks in advance.

Viorel Florian
  • 594
  • 9
  • 29
AnonymousDev
  • 247
  • 3
  • 15
  • Maybe similar question http://stackoverflow.com/questions/14596599/run-command-prompt-as-administrator – boly38 Nov 10 '15 at 20:12
  • yes it is,but i am not getting how to use elevate in my case , It would be good if u can help me regarding that @boly38 – AnonymousDev Nov 10 '15 at 20:54
  • And this one ? http://stackoverflow.com/q/24887927/1827276 – boly38 Nov 10 '15 at 21:37
  • its not working too, i tried that, there is issue regarding access rights, mysqldump command doesnot work without run as admin,I tried mysqldump with normal cmd, it gives error of access denied. – AnonymousDev Nov 10 '15 at 22:47
  • Are you sure this error occurs because of missing admin rights ? I see no reason why a database connection should fail because if wrong Windows permissions. – Marged Nov 11 '15 at 05:28
  • agree. use same windows user as your java app to execute mysqldump to test it. – boly38 Nov 11 '15 at 07:57
  • yes ,I got the reason ,I was trying to right file in C: where my Os is installed and for that reason it was asking for admin right ,after changing path to other drive it worked fine. Thanks guys for your help. – AnonymousDev Nov 11 '15 at 09:08

1 Answers1

1

You can use runas /savecred to execute, but I suggest you use JNI instead:

CreateProcessWithLogonW

CreateProcessAsUser

You can find examples here:

http://blogs.msdn.com/b/alejacma/archive/2007/12/20/how-to-call-createprocesswithlogonw-createprocessasuser-in-net.aspx

cshu
  • 5,654
  • 28
  • 44
  • Will my statement look like this ?? Runtime.getRuntime().exec(new String[] { "cmd.exe ","/c","runas /savecred", executeCmd }) – AnonymousDev Nov 11 '15 at 09:11
  • 2
    @Varun Should be like this: Runtime.getRuntime().exec("cmd /c \"runas /savecred /user:theDomain\\administrator yourCommand\""); – cshu Nov 11 '15 at 09:32
  • okay this is nice...! thanks for clarifying this :D – gumuruh Aug 23 '22 at 04:52