![](/img/trans.png)
[英]How to run a Java application as Windows service using WinRun4J
[英]How to create a windows service with winrun4j
我一直在閱讀文檔,但我無法啟動和停止服務。
我的.ini文件是:
main.class=test.TestService
service.class=test.TestService
service.id=StreamServer
service.name=StreamServer
service.description=Servidor que proporciona una comunicación con streams.
service.controls=stop
classpath.1=*.jar
TestService類是:
package test;
public class TestService{
private static TestServer server;
public static void main (String[] args){
if (args.length == 1){
if (args[0].equals ("start")){
if (server == null){
server = new TestServer (5000);
server.start ();
}
}else if (args[0].equals ("stop")){
if (server != null){
server.stop ();
server = null;
}
}
}
}
}
我必須修改這個類,但我不知道如何。
謝謝。
從winrun4j網站的首頁看一下示例服務:
package org.boris.winrun4j.test;
import org.boris.winrun4j.AbstractService;
import org.boris.winrun4j.EventLog;
import org.boris.winrun4j.ServiceException;
/**
* A basic service.
*/
public class ServiceTest extends AbstractService
{
public int serviceMain(String[] args) throws ServiceException {
int count = 0;
while (!shutdown) {
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
}
if (++count % 10 == 0)
EventLog.report("WinRun4J Test Service", EventLog.INFORMATION, "Ping");
}
return 0;
}
}
啟動服務時將調用serviceMain方法。 在服務准備好關閉之前,您不應該從此方法返回。 還要檢查“shutdown”標志 - 當您在服務控制面板中單擊“停止”(或需要停止服務時)時,此標志將設置為true。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.