我正在尝试将一个简单的 java 应用程序作为 Windows 服务运行。这是我的主要类文件。我正在尝试每五秒钟打印一次时间,并在一分钟后停止。包名称是 samPack。
package samPack;
import java.util.Date;
public class Time {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread thread = new Thread()
{
public void run()
{
long start = System.currentTimeMillis();
long end = start + 60*1000;
while (System.currentTimeMillis() < end){
//System.out.println("Hello World");
Date date = new Date();
System.out.println("Ram Test ---- > "+date.toString());
try
{
Thread.sleep(5000); // 1 second
} catch (Exception e)
{
e.printStackTrace();
}
}
}
};
thread.start();
}
}
为了使其成为 Windows 服务,我下载了 Tanuki Software 的 Java Service Wrapper。并解压桌面上的文件夹。我为上述代码创建了一个可运行的 jar 文件,并将其粘贴到 Java 服务 Wrapper 的 bin 文件夹和 lib 文件夹中。bin 文件夹中的 DemoApp.bat 失败我已将其更改为 run.bat,然后在该文件中我已将变量 WRAPPER_CONF_DEFAULT 更改为 ../conf/wrapper.conf。
在 Wrapper.conf 文件中,这些是我所做的更改
# Java Main class. This class must implement the WrapperListener interface
# or guarantee that the WrapperManager class is initialized. Helper
# classes are provided to do this for you. See the Integration section
# of the documentation for details.
wrapper.java.mainclass=samPack.Time
# Java Classpath (include wrapper.jar) Add class path elements as
wrapper.java.classpath.1=../lib/wrappertest.jar
wrapper.java.classpath.2=../lib/wrapper.jar
wrapper.java.classpath.3=../lib/sam.jar
# Name of the service
wrapper.name=samtestservice
# Display name of the service
wrapper.displayname=Sam Test Service
# Description of the service
wrapper.description=Sam Test Service Decs
# Mode in which the service is installed. AUTO_START, DELAY_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START
现在,当我运行 bat 文件 run.bat 时,它正在控制台中运行并显示以下结果。
wrapper | Launching a JVM...
jvm 1 | Ram Test ---- > Mon May 05 18:18:59 IST 2014
jvm 1 | Ram Test ---- > Mon May 05 18:19:04 IST 2014
jvm 1 | Ram Test ---- > Mon May 05 18:19:09 IST 2014
jvm 1 | Ram Test ---- > Mon May 05 18:19:14 IST 2014
jvm 1 | Ram Test ---- > Mon May 05 18:19:19 IST 2014
jvm 1 | Ram Test ---- > Mon May 05 18:19:24 IST 2014
wrapper | Startup failed: Timed out waiting for a signal from the JVM.
wrapper |
wrapper | ------------------------------------------------------------------------
wrapper | Advice:
wrapper | The Wrapper consists of a native component as well as a set of classes
wrapper | which run within the JVM that it launches. The Java component of the
wrapper | Wrapper must be initialized promptly after the JVM is launched or the
wrapper | Wrapper will timeout, as just happened. Most likely the main class
wrapper | specified in the Wrapper configuration file is not correctly initializing
wrapper | the Wrapper classes:
wrapper | samPack.Time
wrapper | While it is possible to do so manually, the Wrapper ships with helper
wrapper | classes to make this initialization processes automatic.
wrapper | Please review the integration section of the Wrapper's documentation
wrapper | for the various methods which can be employed to launch an application
wrapper | within the Wrapper:
wrapper | http://wrapper.tanukisoftware.com/doc/english/integrate.html
wrapper | ------------------------------------------------------------------------
我已经通过运行 bat 文件安装了该服务,但无法启动该服务。服务开始。怎么了?