Recently, I want to find a software to control the shutdown time of my computer. I have found several software on the Internet that can be used to set the specific shutdown time on the visual interface. Because I want to write Shutdown Program It is running on another machine. It can only be accessed at pm to PM, and automatically shut down at pm. In order to make others feel that the software "exists" (so that users do not close the Timed Shutdown software themselves), I want to register the shutdown software as a service and run it in the background. This section describes how to use the assumervice software to register a Java program as a Windows service. I. Use javaservice to register Java program for Windows Service [1] Download javaservice visit the Web site http://javaservice.objectweb.org/download the Windows version of The javaservice file, I download is the javaservice-2.0.10.rar, the latest version is "2.0.10 ". [2] install assumervice unzip the downloaded assumervices to a directory, I decompress it to the Directory D:/software/JavaService-2.0.10 (decompress it to any directory, it is best not to decompress the package to the Chinese directory, saving problems.) [3] compile a Timed Shutdown Code , See the Timed Shutdown code in chapter 2. 1) for specific code, see Chapter 2. The class name is com. test. timer. timershutdownwindows2) export the written Java file to the form of class, the export class to the directory "D:/software/JavaService-2.0.10/classes/COM/test/timer. That is to put the exported com package under the "D:/software/JavaService-2.0.10/classes" directory. [4] register the Java program for the Windows service to go to the "D:/software/JavaService-2.0.10" directory and execute the following command: assumervice.exe-install myshutdownservice "% java_home %"/JRE/bin/Server/JVM. DLL-djava. class. path = "% java_home %"/lib/tools. jar; D:/software/JavaService-2.0.10/classes-start COM. test. timer. in timershutdownwindows, "-install" is followed by the service name, and "-start" is followed by the class name to be started, "djava. class. the "D:/software/JavaService-2.0.10/Classe" address in the parameter following path is my "timershu The path of the tdownwindows class, which can be changed to your classpath in actual application. Note the following points: 1) "% java_home %" JDK directory. If the JDK directory is not configured, replace it with the actual absolute address of JDK. 2)-djava. class. PATH is required because the system's classpath variable cannot be accessed when the service is started, so it must be declared here. If there are many jar files, to avoid writing too long commands, we can use "-djava. ext. dirs = jars Directory "parameter. 3) after adding a service, you can enter "services. MSC command to view all services, and can modify the Service Startup Type (automatic start or manual start. [5] Test 1) start the service. After registering the service, Run "Net start myshutdownservice" to start the service, after the service is started, the my_shutdown.log file is generated in the root directory of drive D. 2) If you want to disable the service, run the "net stop myshutdownservice" command to disable the service. 3) to delete a service, run the "SC Delete myshutdownservice" command to delete the service. Ii. Timed Shutdown code package COM. test. timer; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. io. printwriter; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; public class timershutdownwindows {/* interval of time for detecting shutdown */Private Static long m_ndetectinterval = 5000;/* record the time of the last detection, in milliseconds */Private Static long m_llastmilliseconds = 0;/* Minimum hours for computers */ Private Static int m_nusepcminhour = 17;/* maximum computer hours */Private Static int m_nusecomputermaxhour = 23;/* If the time exceeds this time, shut down the computer */Private Static int m_nminutes = 25;/* save location of the log file */Private Static string m_slogfile = "D:" + file. separator + "my_shutdown.log";/* record whether the current system has enabled the automatic shutdown Program */Private Static Boolean bhasshutdownpc = false; /*** @ Param ARGs */public static void main (string [] ARGs ){// 1. start a thread separately to check thread athread = new thread (New timerdetector (); athread. start ();}/*** defines the internal class ** @ author administrator **/static class timerdetector implements runnable {/** (non-javadoc) ** @ see Java. lang. runnable # Run () */Public void run () {// 1. obtain the log file printwriter out = NULL; simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); try {out = new printwriter (New filewriter (M_slogfile, true), true);} catch (ioexception E1) {out = NULL; e1.printstacktrace ();} // 2. record Service start time appendlog (Out, "service start time:" + DF. format (new date (); While (true) {// 1. boolean bshoudshutdownpc = validateshoudshutdownpc (out); If (bshoudshutdownpc) {// If the verification fails, Force Shutdown exectueshudown (out );} else {bhasshutdownpc = false;} // 2. try {thread. sleep (m_ndetectinterval);} c Atch (interruptedexception e) {appendlog (Out, E. getmessage () ;}}/ *** verify whether the current time is the time needed for shutdown ** @ return */private Boolean validateshoudshutdownpc (printwriter _ out) {// 1. determines whether the system time has been modified. boolean bhasmodifysystemtime = hour (_ out); appendlog (_ out, "bhasmodifysystemtime:" + bhasmodifysystemtime); If (bhasmodifysystemtime) {return bhasmodifysystemtime ;} // 2. if the system time is not modified, check whether the current time has exceeded the specified time. Time boolean bshoudsleep = nowissleeptime (); appendlog (_ out, "bshoudsleep:" + bshoudsleep); If (bshoudsleep) {return bshoudsleep;} return false ;} /*** determine whether the current time should be rested ** @ return */private Boolean nowissleeptime () {// 1. obtain the current hour and minute calendar acalendar = calendar ar. getinstance (); int nhour = acalendar. get (calendar. hour_of_day); int nminute = acalendar. get (calendar. minute); // 2. determine whether a PC can be used in the current hour In the time, the maximum hour is 23 if (nhour <m_nusepcminhour) {return true;} // you need to judge it separately, if (nhour> = m_nusecomputermaxhour) & (nminute> = m_nminutes) {return true;} // 3. non-rest time return false;}/*** determines if someone has modified the system time. If someone has modified the system time, true is returned, <br> * Otherwise, false ** @ return */private Boolean detectmodifysytemtime (printwriter _ out) {// 1. first detection system time if (m_llastmilliseconds = 0) {m_llastmilliseconds = sys TEM. currenttimemillis (); Return false;} // 2. the difference between the two times is long linteral = system. currenttimemillis ()-m_llastmilliseconds; linteral = math. ABS (linteral); // 3. determine the interval of two times. The two results are not necessarily equal to m_ndetectinterval, and the allowable error is 1 minute long lmaxinterval = m_ndetectinterval + 60*1000; appendlog (_ out, "linteral ::: "+ linteral); appendlog (_ out," lmaxinterval: "+ lmaxinterval); If (linteral> lmaxinterval) {// someone has modified the system Time, Force Shutdown return true;} // 4. the last detection time m_llastmilliseconds = system is recorded only when no one modifies the time. currenttimemillis (); Return false ;} /*** write log information in the specified stream ** @ Param _ outwriter * @ Param _ sappendcontent */private void appendlog (printwriter _ outwriter, string _ sappendcontent) {If (_ outwriter = NULL) {return;} _ outwriter. println (_ sappendcontent);}/*** run the shutdown command */private void exectueshudown (printwriter _ out) {If (B Hasshutdownpc) {simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); appendlog (_ out, "the system is about to close. Current Time:" + DF. format (new date (); return;} appendlog (_ out, "Someone modified the system time and the system forced shutdown! "); // Shut down try {runtime.getruntime(cmd.exe C (" shutdown-s-t 120-C/"very late. It's time to go to bed and shut down the computer two minutes later. /"");} Catch (ioexception e) {appendlog (_ out, E. getmessage ();} bhasshutdownpc = true ;}}}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.