B4J Question Set Date and Time from code

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
it's possible with B4j change The System Date and Time directly from code?
Thanks
 

micro

Well-Known Member
Licensed User
Longtime User
Thanks for your answer, my question is for raspberry (linux system).
Best regards
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
my question is for raspberry (linux system)
The solution will be the same; using JShell.

But i dont know the command you need to use on a Raspberry PI to set the time.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
is correct formatting this command?

B4X:
Dim js As  Shell
js.Initialize("", "date", Array As String("-s", d))
js.Run(-1)

Where d = "Sat Jan 30 08:10:12 CET 2016"

If i send the command from putty it's work but with b4j not (On Raspberry in debug mode, other commands work).
Thanks
 
Upvote 0

billzhan

Active Member
Licensed User
Longtime User
I guess the sudo is needed here. The default user 'pi' doesn't have the priv to change system settings.
B4X:
'try
js.Initialize("", "sudo", ArrayAsString("date","-s", d))

'or you should start the jar ( your complied jar/ b4j_bridge ) with sudo
 
Last edited:
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
It should work if you are running *.jar with root account.
yes, I start the jar wit root account but strangely does not work.
I tried also with ssh library (for B4A)
B4X:
Sub SetSSH
    Try
        ssh.Initialize("ssh", "127.0.0.1", 22)
        ssh.DebugOutput = False
        ssh.authenticateWithPassword("pi", "raspberry")
        ssh.execCommand("sudo date -s " & d, 10)
    Catch As Exception
        Log(LastException.Message)
    End Try
End Sub
but the problem is the same.

It's possible set the date from file with -f option?
 
Last edited:
Upvote 0

billzhan

Active Member
Licensed User
Longtime User
You need to enable the super user "root” ( and change PermitRootLogin if you use ssh ). sudo may not work
ssh.authenticateWithPassword("root", "rootpasswd")

http://www.raspberry-projects.com/pi/command-line/root-user-privileges


Enabling The Root Account
By default the root account is disabled, but you can enable it by using this command and giving it a password


sudo passwd root
Now you can log into your pi as the root user. Whilst this opens up security concerns its sometimes very useful, for instance when developing with netbeans and remotely running applicaiotn that access the IO pins.

If it doesn't work check the ssh config


sudo nano /etc/ssh/sshd_config

Search for PermitRootLogin and change it to yes

You can undo logging in as root again using this command

sudo passwd -l root

 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Thanks for all
but why with putty and simply with "sudo date -s xxxxxxxxxxxxxxx" it's work?
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Sudo java -jar b4j-bridge.jar > sdtout.txt 2> sdterr.txt

Or do I need another set?
 
Last edited:
Upvote 0
Top