changing the system date from javacode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ksrashmi
    New Member
    • May 2007
    • 30

    changing the system date from javacode

    Hi ,

    I want to change the system date using java/jsp . i am using linux system .

    public class LinuxJava {
    public static void main(String[] args) {
    try {
    String command = "date 07212008";
    final Process process = Runtime.getRunt ime().exec(comm and);

    // int returnCode = process.waitFor ();
    // process.toStrin g()
    System.out.prin tln("Return code = " + process.toStrin g());
    } catch (Exception e) {
    e.printStackTra ce();
    }
    }

    }

    i tried this code it didn't change the code . can any one tell how to change the system date with java application ?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What *did* it do? What was the completion code of the process? Any exceptions
    thrown? Is the effective user allowed to change the system date?

    kind regards,

    Jos

    Comment

    • ksrashmi
      New Member
      • May 2007
      • 30

      #3
      no exception thrown it is executing the code but it is not chaning the date .

      for chaning the date we need to have root permision . may be beacuse of that it is not executing .

      i tried with this command also

      String command = "date -s 'Sat May 24 14:43:00 PDT 2005' "; in place of
      String command = "date 07212008'

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by ksrashmi
        no exception thrown it is executing the code but it is not chaning the date .

        for chaning the date we need to have root permision . may be beacuse of that it is not executing .
        Sure, you have to be root to change the system date so you have to run your
        little program with an effective uid set to root.

        kind regards,

        Jos

        Comment

        • samido
          New Member
          • Oct 2007
          • 52

          #5
          I am not sure what this suppose to do but the type of exception is as follow, somebody please help out so that we can all learn this thign

          java.io.IOExcep tion: Cannot run program "date": CreateProcess error=2, The system cannot find the file specified
          at java.lang.Proce ssBuilder.start (Unknown Source)
          at java.lang.Runti me.exec(Unknown Source)
          at java.lang.Runti me.exec(Unknown Source)
          at java.lang.Runti me.exec(Unknown Source)
          at Test.main(Test. java:16)
          Caused by: java.io.IOExcep tion: CreateProcess error=2, The system cannot find the file specified
          at java.lang.Proce ssImpl.create(N ative Method)
          at java.lang.Proce ssImpl.<init>(U nknown Source)
          at java.lang.Proce ssImpl.start(Un known Source)
          ... 5 more

          kind regarde:

          Sam Rabophala

          Comment

          • ksrashmi
            New Member
            • May 2007
            • 30

            #6
            i have changed the class file user as root but still it is not working .
            anything else i need to do ?



            Originally posted by JosAH
            Sure, you have to be root to change the system date so you have to run your
            little program with an effective uid set to root.

            kind regards,

            Jos

            Comment

            • ksrashmi
              New Member
              • May 2007
              • 30

              #7
              i tried by writing the one shell script with root permision .

              # Author: Rashmi
              # date : 01-07-2005

              date -s 'May 12 2008'


              and calling that one with java code

              public class LinuxJava {
              public static void main(String[] args) {
              try {
              String command = "/home/rashmi/test.sh ";
              final Process process = Runtime.getRunt ime().exec(comm and);

              // int returnCode = process.waitFor ();
              // process.toStrin g()
              System.out.prin tln("Return code = " + process.toStrin g());

              } catch (Exception e) {
              e.printStackTra ce();
              }
              }

              }


              it is also executing .. but not chaning the date :(

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by ksrashmi
                i tried by writing the one shell script with root permision .

                [ ... ]

                it is also executing .. but not chaning the date :(
                If you run that script manually, does it change the date?

                kind regards,

                Jos

                Comment

                • ksrashmi
                  New Member
                  • May 2007
                  • 30

                  #9
                  now i am able to change the date , i changed the tomcat user to root . and script user to root . now it started working .
                  thanks

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by ksrashmi
                    now i am able to change the date , i changed the tomcat user to root . and script user to root . now it started working .
                    thanks
                    Oh so Tomcat was involved in all this? I didn't pick that up from your ealier posts in this thread.

                    Comment

                    • ksrashmi
                      New Member
                      • May 2007
                      • 30

                      #11
                      i wanted do this either by the stand alone java program or from the application .
                      after chaning the tomcat to root user , i called the java program program from jsp . then it started working .

                      Comment

                      • pradeepkhyd07
                        New Member
                        • Aug 2016
                        • 1

                        #12
                        hi ksrashmi,

                        can you please paste the content that was there in
                        /home/rashmi/test.sh.

                        Thanks,
                        Pradeep.

                        Comment

                        Working...