Results 1 to 8 of 8

Thread: running a command from java code with sudo

  1. #1
    Join Date
    Jul 2013
    Beans
    9

    running a command from java code with sudo

    hi if i wanted to run a command from netbeans / eclipise i would use htis code:

    Runtime runtime = Runtime.getRuntime();
    runtime.exec("the code u want to execute in the command line");

    however if the command needs sudo how does one pass in the password?
    Last edited by john8; August 9th, 2013 at 08:05 PM.

  2. #2
    Join Date
    Jul 2012
    Beans
    14

    Re: running a command from java code with sudo

    Use gksudo instead of sudo for a graphical password input dialog.

  3. #3
    Join Date
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: running a command from java code with sudo

    For obvious security reasons password less sudo is not recommended. However you can set up sudo such a way that it won't require password for certain commands/scripts.

    You can place your commands("the code u want to execute in the command line") in a shell script and let sudo know to run that script without password.

    Type sudo visudo at the terminal to open the sudo permissions (sudoers) file.

    add an entry like below in the sudoers file, just below the this line. %sudo ALL=(ALL:ALL) ALL

    Code:
    your_username  ALL=(ALL) NOPASSWD: /home/your_username/sudoCommands.sh
    You can also find more information about sudoers on the below link.

    https://help.ubuntu.com/community/Sudoers
    Last edited by codemaniac; July 31st, 2013 at 12:12 PM.
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  4. #4
    Join Date
    Jul 2013
    Beans
    9

    Re: running a command from java code with sudo

    thanks for replys i am ttrying to use gksudo to achieve
    Runtime runtime = Runtime.getRuntime();
    runtime.exec("gksudo commandtorun \npassword \n");
    however this doesint pass the password to the gksudo window whats am i doing wrong ?
    thanks

  5. #5
    Join Date
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: running a command from java code with sudo

    you have to change the sudo configuration to let it run without password for a specific command.
    Please read post #3.
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  6. #6
    Join Date
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: running a command from java code with sudo

    you have to change the sudo configuration to let it run without password for a specific command.
    Please read post #3.
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  7. #7
    Join Date
    Jul 2013
    Beans
    9

    Re: running a command from java code with sudo

    Quote Originally Posted by codemaniac View Post
    you have to change the sudo configuration to let it run without password for a specific command.
    Please read post #3.
    thanks for reply but the problem is i need to run a command from my code not a bash file, everytime the command runs the code is different so i dont think this would work

  8. #8
    Join Date
    Jul 2013
    Beans
    9

    Re: running a command from java code with sudo

    ah i use gksudo to run cmd then used robot class to sendkeys to the window on top solved thanks for all replys all very usfull thanks how does one mark thread as solved

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •