Domanda

I have a java code written to change the system time but it is not working. java code is compiled and run successfully. Someone please point out what's wrong here.

My java code is:

Process process = Runtime.getRuntime().exec("/home/priyatam/timesetter.sh");
process.waitFor();
DataInputStream d = new DataInputStream(process.getInputStream());
System.out.println(d.read());   //It gives the output as number of bytes as 83 

The content of sh file is as follows:

#!/bin/sh

date --set="2014-2-20 11:58 AM"

The command 'date --set="2014-2-20 11:58 AM"' works properly in terminal. Moreover, this sh file works fine when executed from terminal but it won't work through java code.

Anyone have any idea, please share.

È stato utile?

Soluzione

i think you should doit like that

String[] cmd = { "/bin/sh","/home/priyatam/timesetter.sh" };
Process p = Runtime.getRuntime.exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line=null;

while((line=input.readLine()) != null) {

   log.warn(line);
}

int exitVal = process.waitFor();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top