Results 1 to 3 of 3
  1. #1
    manojpant2002 is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default Unable to change the system time in Ubuntu using the given code

    Tried to manully set the system time using date -s "Dec 29 2009",was successfull from terminal(was logged in as root user both the times" ,but was not able to execute the same from code,any help would be higly appreciated

    import java.io.*;
    public class Test
    {
    public static void main(String args[]) {

    try {
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec("date -s \"Dec 29 2009\"" );

    BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

    String line=null;

    while((line=input.readLine()) != null) {
    System.out.println(line);
    }

    int exitVal = pr.waitFor();
    String exitVal1 = pr.getOutputStream().toString();
    System.out.println("Exited with error code "+exitVal);


    } catch(Exception e) {
    System.out.println(e.toString());
    e.printStackTrace();
    }
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Is there any output?

    The Runtime.exec() documentation says "More precisely, the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order." and this suggests the command is tokenised as

    date
    -s
    "Dec
    29
    2009"

    ie the date executable is called with 4 arguments which is not what you mean.

    Try the version of exec() that takes an array of strings.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    (I don't think you need the quotes around the second argument even though you do in a command shell.)

Similar Threads

  1. System Time
    By vasa in forum Threads and Synchronization
    Replies: 7
    Last Post: 08-22-2009, 03:36 PM
  2. setting system clocks time
    By pks in forum New To Java
    Replies: 4
    Last Post: 05-14-2009, 04:36 AM
  3. System time problem
    By dswastik in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-08-2009, 02:36 PM
  4. set system time
    By @eddie.com in forum New To Java
    Replies: 3
    Last Post: 09-02-2008, 04:55 PM
  5. Urgent-Imp-Displaying message with respect to system time
    By garinapavan in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 03:17 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
  •