Results 1 to 8 of 8
  1. #1
    mysticav is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Runtime.getRuntime().exec() and pppd

    need to execute pppd command. The problem is that pppd command has an argument that at the same time uses its own argument. I'm talking about "connect" argument.

    For example:
    Runtime.getRuntime().exec("pppd ttyUSB0 460800 connect '/system/bin/chat -v -f /system/etc/ppp/gprsconnect-chat'");

    As you can see, the connect argument invokes '/system/bin/chat -v -f /system/etc/ppp/gprsconnect-chat'.

    So seems that Runtime.getRuntime().exec() does not know how to deal with that situation. Because it tries to interpret "connect '/system/bin/chat -v -f /system/etc/ppp/gprsconnect-chat'" as a single word.

    I'm new to JAVA and this seems to be a very obvious problem dealing with exec() command.
    Please help.
    Last edited by mysticav; 02-13-2011 at 11:24 PM.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Runtime.exec does not have access to shell commands. If you need to use them, you will have to write a script to execute those commands, and then you can 'exec' the script.

  3. #3
    mysticav is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Do you mean that I have to programatically create a script, and then run the script via exec() ?

    is that possible ?

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    How the script is created doesn't matter. You can create it by hand if you want, or you can have your program write it out dynamically. Runtime.exec does not have an interpreter, so all it can do is execute simple commands with arguments. But it does not interpret the commands.

    I should have mentioned that it is also possible to execute a command interpreter and pass all these arguments to it. I'm not sure what the syntax would be for this case, ut it probably is possible to do it by having the first argument be "command" rather than pppd.

  5. #5
    mysticav is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    So, in case I run a script win exec, can exec('myfile'); do the job ?

    O I need to specify a command before the filename ?

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If it's a proper script with execute permissions and within the runtime path and abides by any restrictions your OS has on script names, then yes. If it wouldn't work to execute it from a terminal, then it also won't work trying to execute it with Runtime.exec().

  7. #7
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    You shouldn't need another script. What OS are you on? For windows:

    Java Code:
    String[] cmds = new String[3];
    cmds[0] = "cmd"; //interpreter
    cmds[1] = "/c"; //it is a command line program
    cmds[2] = "pppd ttyUSB0 460800 connect '/system/bin/chat -v -f /system/etc/ppp/gprsconnect-chat'";
    
    Runtime.getRuntime().exec(cmds);
    It might be -c instead of /c

  8. #8
    mysticav is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    I'm working on Android.

    I was able to manage the syntax problem based on your suggestions. But then, I had problems with pppd permissions, because Runtime.getRuntime().exec() runs with limited permissions.

    So after hours of searching, I found a very nice Class that wraps Runtime.getRuntime().exec() in some way that can handle commands that require root permissions. Of course, the machine (phone or tablet pc) should be rooted in order to use this class.

    This is the class:
    ShellInterface.java - market-enabler - Project Hosting on Google Code

    Thank you all for your help.

Similar Threads

  1. Replies: 5
    Last Post: 07-07-2010, 12:50 PM
  2. Slow Output reading with Runtime.getRuntime().exec()
    By gmcouto in forum Advanced Java
    Replies: 8
    Last Post: 05-14-2010, 10:42 AM
  3. problem with Runtime.getRuntime().exec when running java in .bat
    By Shayko in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-27-2010, 07:46 PM
  4. help with Runtime.getRuntime().exec
    By collin389 in forum AWT / Swing
    Replies: 3
    Last Post: 11-09-2009, 04:22 AM
  5. Problem with Runtime.getRuntime().exec with Linux Commands
    By swapnilnawale in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-23-2009, 10:23 PM

Posting Permissions

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