无法将Windows应用程序作为Windows服务运行

时间:2014-05-05 12:56:42

标签: java windows batch-file java-service-wrapper

我正在尝试将一个简单的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文件夹中。 DefileApp.bat在bin文件夹中失败我已将其更改为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文件安装了该服务但无法启动该服务。 服务dint开始。 什么出错了?

1 个答案:

答案 0 :(得分:2)

wrapper.java.mainclass=samPack.Time修改为wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp

并添加新行wrapper.app.parameter.1=samPack.Time

详情请参阅Tanuki网站:http://wrapper.tanukisoftware.com/doc/english/integrate-simple-win.html