469,271 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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.getRuntime().exec(command);

// int returnCode = process.waitFor();
// process.toString()
System.out.println("Return code = " + process.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

}

i tried this code it didn't change the code . can any one tell how to change the system date with java application ?
Jul 21 '08 #1
11 10712
JosAH
11,448 Expert 8TB
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
Jul 21 '08 #2
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'
Jul 21 '08 #3
JosAH
11,448 Expert 8TB
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
Jul 21 '08 #4
samido
52
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.IOException: Cannot run program "date": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Test.main(Test.java:16)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more

kind regarde:

Sam Rabophala
Jul 21 '08 #5
i have changed the class file user as root but still it is not working .
anything else i need to do ?



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
Jul 21 '08 #6
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.getRuntime().exec(command);

// int returnCode = process.waitFor();
// process.toString()
System.out.println("Return code = " + process.toString());

} catch (Exception e) {
e.printStackTrace();
}
}

}


it is also executing .. but not chaning the date :(
Jul 21 '08 #7
JosAH
11,448 Expert 8TB
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
Jul 21 '08 #8
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
Jul 24 '08 #9
r035198x
13,262 8TB
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.
Jul 24 '08 #10
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 .
Jul 24 '08 #11
hi ksrashmi,

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

Thanks,
Pradeep.
Aug 4 '16 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by John Smith | last post: by
1 post views Thread by Robert Bravery | last post: by
5 posts views Thread by Mark Fox | last post: by
2 posts views Thread by barry | last post: by
20 posts views Thread by andreas | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.