Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the time the computer was turned ON using Java [duplicate]

Tags:

java

startup

I have a Java JAR program that is run on Windows startup. It needs to register the time the computer was turned on, so naturally it simply gets the time when the main method is called. Works fine.

But, some of the computers we installed the program in are very (very) slow. It is unsurprising that the startup of the program may occur after several minutes upon turning on the computer.

An inaccuracy of 10-15 seconds is acceptable (our newest computers). But two or three minutes in the oldest ones not quite so.

Is there a reasonable way to determine the time the computer was turned ON? Using Java?

I do realize that for this kind of job, Java was a terrible choice. It is well known that Java startup is not particularly fast at all. I'll ask this anyway since the program is already done with Java - but if there is no solution I'll work on a C++ version.

Windows.

like image 840
Voldemort Avatar asked Feb 13 '26 18:02

Voldemort


2 Answers

What about write short *.bat file, with only one functionality - after start it writes actual time to a file.

When your Java program is loaded, it just first try to read file and find out the time of computer start.


Maybe even better idea - start your program through .bat file. First get actual time and save it to variable, then start program with value of this variable as parameter. When the program is on, it will first recieve the time of start in main method.

like image 134
libik Avatar answered Feb 15 '26 06:02

libik


There are several options to find this out on Windows:

systeminfo | find /i "Boot Time"

(As noted by @Robert this only works on English Windows installations)

or

wmic os get lastbootuptime

or

net statistics workstation

Just use Runtime.getRuntime().exec("command"); to call the command.

like image 30
Adriaan Koster Avatar answered Feb 15 '26 06:02

Adriaan Koster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!