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

时间:2015-06-02 11:36:16

标签: java windows service windows-services

我使用教程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

在我的stderror文件中,报告了以下内容:

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.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.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;

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

  

MyService1服务因服务特定错误而终止系统无法打开文件。

我经常搜索服务程序应该包含哪些但没有运气。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

文件夹结构:

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

Java Class(守护程序类):

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

以下是我班级的片段。

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");
}

帮助我让我的服务处于运行状态的链接:http://developeriq.in/articles/2012/may/01/transforming-java-programs-into-windows-services-u/