无法 运行 用户服务。异常:启动失败 Java,ServiceStart 返回 4

Unable to run user service. Exception: Failed to start Java, ServiceStart returned 4

我使用教程创建了一个服务http://web.archive.org/web/20090228071059/http://blog.platinumsolutions.com/node/234

我创建了如下文件:

C:\MyService1

\bin
    \myService1.exe
    \myService1w.exe
\classes
    \com\services\MyService1.class
    \HelloWorld.jar
\logs

现在当我执行 myService1.exe 时,服务没有启动。 我在日志中收到以下消息

[2015-06-02 15:23:29] [info]  [ 3968] Commons Daemon procrun (1.0.15.0 32-bit) started
[2015-06-02 15:23:29] [info]  [ 3968] Running 'MyService1' Service...
[2015-06-02 15:23:29] [info]  [ 4204] Starting service...
[2015-06-02 15:23:29] [error] [ 5372] FindClass com/services/MyService1 failed
[2015-06-02 15:23:29] [error] [ 4204] Failed to start Java
[2015-06-02 15:23:29] [error] [ 4204] ServiceStart returned 4
[2015-06-02 15:23:29] [info]  [ 3968] Run service finished.
[2015-06-02 15:23:29] [info]  [ 3968] Commons Daemon procrun finished

在我的标准错误文件中,报告如下:

2015-06-02 15:23:29 Commons Daemon procrun stderr initialized
java.lang.NoClassDefFoundError: com/services/MyService1 (wrong name: services/MyService1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access0(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Exception in thread "main" 

我的环境变量如下:

 CLASSPATH: .;%JAVA_HOME%\jre\lib;
 JAVA_HOME: C:\Program Files\Java\jdk1.7.0;
 Path: %JAVA_HOME%\bin;

当我检查事件查看器时,我得到:

The MyService1 service terminated with service-specific error The system cannot open the file.

我搜索了很多服务程序实际应该包含的内容,但没有找到。

我做了以下更改以使我的服务成功运行。

文件夹结构:

C:\MyService1
    \myService1.exe
    \myService1w.exe
    \MyService1.class
    \HelloWorld.jar
    \logs

Java Class(守护程序 Class):

  • 在默认包
  • 中创建javaclass
  • class有两个静态方法:start()和stop()
  • start() 应该有一个无限循环

下面是我 class 的片段。

static boolean isRunning = true;
static boolean isJobsRunning = false;

@SuppressWarnings("static-access")
public static void start(String []args) {
    String command = "run.bat";
    System.out.println("Started JobServices successfully");
    while(isRunning){
        if(!isJobsRunning) { //the job should run only once after the service is started
            try {
                System.out.println("Running batch file");
                Runtime.getRuntime().exec(command);
                isJobsRunning = true;
                System.out.println("Executed batch file successfully");
            } catch (IOException e) {
                System.err.println("Unable to execute Jobs jar");
                e.printStackTrace();
            }
        }
    }
    System.out.println("Completed JobService");

}

public static void stop(String args[]) {
     if(!isRunning) 
         return;

     isRunning = false;
     System.out.println("JobService Stopped successfully");
}

Link 帮助我在 运行 状态下获得服务:http://developeriq.in/articles/2012/may/01/transforming-java-programs-into-windows-services-u/

我最近在 运行 spring 使用 procrun 将应用程序作为服务启动时遇到了同样的问题。 我的错误是我将主要 class 的路径作为启动 class。当我使用 org.springframework.boot.loader.JarLauncher 作为启动 class 时,这个问题得到了解决。您应该用作启动 class 的 class 可以在 MANIFEST.MF 罐子里面