Results 1 to 10 of 10

Thread: How to run commands with parameters in Java with gksudo?

  1. #1
    Join Date
    Dec 2009
    Location
    Romania
    Beans
    7
    Distro
    Ubuntu 9.10 Karmic Koala

    Question How to run commands with parameters in Java with gksudo?

    Hi,

    I want to run the gksudo lshw -short command from Java.

    Code:
    gksudo lshw
    works fine, but I don't need that much information.

    I've tried
    Code:
    gksudo lshw\\ -short
    gksudo 'lshw -short'
    gksudo \"lshw -short\"
    as Java Strings but none of them work.


    They all work fine from the command line.
    I know I can parse the result of lshw but I don't want to if there is a way to run gksudo lshw -short from Java.

    Anyone know how/if it can be done?

    Thanks!
    Last edited by Tommy599; April 25th, 2010 at 09:56 PM.

  2. #2
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: How to run commands with parameters in Java with gksudo?

    try
    Code:
    gksudo -- lshw -short

  3. #3
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: How to run commands with parameters in Java with gksudo?

    how are you running them from Java?
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  4. #4
    Join Date
    Dec 2009
    Location
    Romania
    Beans
    7
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: How to run commands with parameters in Java with gksudo?

    Quote Originally Posted by geirha View Post
    try
    Code:
    gksudo -- lshw -short
    Wow it works!

    Thank you very much!

    Can I also ask why it works? What does the -- do?

  5. #5
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: How to run commands with parameters in Java with gksudo?

    generally -- means 'stop processing command line options'
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  6. #6
    Join Date
    Dec 2009
    Location
    Romania
    Beans
    7
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: How to run commands with parameters in Java with gksudo?

    Quote Originally Posted by slavik View Post
    how are you running them from Java?
    I have the commands in an array of Strings and pass them to
    Code:
    runtime.exec(commands[i]);

  7. #7
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: How to run commands with parameters in Java with gksudo?

    that invokes the shell, I bet. Look into spawning a separate process.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  8. #8
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: How to run commands with parameters in Java with gksudo?

    Unfortunately the gksudo man-page does not document the special -- option, though it is fairly common for commands to accept it. As slavik mentioned, it tells the app to stop reading options. Without it, gksudo will treat any argument starting with a - as an option to gksudo. When it encounters --, all arguments following will no longer be treated as options to gksudo, and just passed on to the child process.

    If you ask me, this behavior is absurd; the sudo command does not behave this way, but, at least there's a way to work around it.

    Oh and according to the javadoc, Runtime.exec does not run the commands in a shell, which would explain why the attempts of quoting did not work.
    Last edited by geirha; April 25th, 2010 at 11:11 PM.

  9. #9
    Join Date
    Dec 2009
    Location
    Romania
    Beans
    7
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: How to run commands with parameters in Java with gksudo?

    Quote Originally Posted by slavik View Post
    that invokes the shell, I bet. Look into spawning a separate process.
    I can get the output I want from it, why change it?

    And what do you mean by separate process? Like a background process or a new Java thread?

  10. #10
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: How to run commands with parameters in Java with gksudo?

    Quote Originally Posted by Tommy599 View Post
    I can get the output I want from it, why change it?

    And what do you mean by separate process? Like a background process or a new Java thread?
    I mean fork/exec.

    as for --, man getopt
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

Tags for this Thread

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
  •