java.lang.ClassNotFoundException:org.zeromq.ZContext 尝试启动 windows 服务时

java.lang.ClassNotFoundException: org.zeromq.ZContext when trying to start windows service

我有一个我创建的基本 Maven java 应用程序,它依赖于 JeroMQ,它是 ZeroMQ 的完整 Java 实现。由于我还需要将此 java 应用程序包装为 windows 服务,因此我选择使用 Apache Commons Daemon,具体来说,遵循了这个优秀的示例:http://web.archive.org/web/20090228071059/http://blog.platinumsolutions.com/node/234 这是 Java代码如下:

package com.org.SubscriberACD;

import java.nio.charset.Charset;

import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;

/**
 * JeroMQ Subscriber for Apache Commons Daemon
 *
 */
public class Subscriber 
{
    /**
    * Single static instance of the service class
    */
    private static Subscriber subscriber_service = new Subscriber();

    /**
     * Static method called by prunsrv to start/stop
     * the service.  Pass the argument "start"
     * to start the service, and pass "stop" to
     * stop the service.
     */
    public static void windowsService(String args[]) {
       String cmd = "start";
       if(args.length > 0) {
          cmd = args[0];
       }

       if("start".equals(cmd)) {
          subscriber_service.start();
       }
       else {
          subscriber_service.stop();
       }
    }

    /**
     * Flag to know if this service
     * instance has been stopped.
     */
    private boolean stopped = false;


    /**
     * Start this service instance
     */
    public void start() {

       stopped = false;

       System.out.println("My Service Started "
                          + new java.util.Date());

       ZContext context = new ZContext();

       Socket subscriber = context.createSocket(ZMQ.SUB);
       subscriber.connect("tcp://localhost:5556");
       String subscription = "MySub";
       subscriber.subscribe(subscription.getBytes(Charset.forName("UTF-8")));

       while(!stopped) {
          System.out.println("My Service Executing "
                              + new java.util.Date());

          String topic = subscriber.recvStr();
          if (topic == null)
              break;
          String data = subscriber.recvStr();
          assert(topic.equals(subscription));
          System.out.println(data);

          synchronized(this) {
             try {
                this.wait(60000);  // wait 1 minute
             }
             catch(InterruptedException ie){}
          }
       }

       subscriber.close();
       context.close();
       context.destroy();

       System.out.println("My Service Finished "
                           + new java.util.Date());
    }

    /**
     * Stop this service instance
     */
    public void stop() {
       stopped = true;
       synchronized(this) {
          this.notify();
       }
    }
 }

然后我按照教程建议创建了以下文件夹结构:

E:\SubscriberACD
   \bin
        \subscriberACD.exe
        \subscriberACDw.exe
   \classes
        \com\org\SubscriberACD\Subscriber.class
   \logs

然后我导航到 bin 目录并发出以下命令来安装服务:

subscriberACD.exe //IS//SubscriberACD --Install=E:\SubscriberACD\bin\subscriberACD.exe --Descriptio
n="Subscriber using Apache Commons Daemon" --Jvm=c:\glassfish4\jdk7\jre
\bin\server\jvm.dll --Classpath=E:\SubscriberACD\classes --StartMode=jvm
 --StartClass=com.org.SubscriberACD.Subscriber --StartMethod=windowsSer
vice --StartParams=start --StopMode=jvm --StopClass=com.org.SubscriberA
CD.Subscriber --StopMethod=windowsService --StopParams=stop --LogPath=E:\SubscriberACD\logs --StdOutput=auto --StdError=auto

安装工作正常,因为我可以在 Windows 服务中看到它。但是,当我尝试从那里启动它时,我收到一条错误消息 "Windows cannot start the SubscriberACD on Local Computer".

我检查了错误日志并看到以下条目:

2016-04-14 14:38:40 Commons Daemon procrun stderr initialized
Exception in thread "main" ror: org/zeromq/ZContext
    at com.org.SubscriberACD.Subscriber.start(Subscriber.java:57)
    at com.org.SubscriberACD.Subscriber.windowsService(Subscriber.java:33)
Caused by: java.lang.ClassNotFoundException: org.zeromq.ZContext
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 2 more

值得注意的是,JeroMQ 目前是我的 Maven 依赖项下的一个 jar。我从我的 POM.xml 文件配置它。

我认为问题可能是我的服务无法访问我的 Maven 依赖项下的 JeroMQ jar。我的假设是 class 文件不包含依赖项。所以我尝试的是将我的整个项目导出为一个 jar 并将那个婴儿卡在 E:\SubscriberACD\classes\ 所以我的结构现在看起来像这样:

E:\SubscriberACD
   \bin
        \subscriberACD.exe
        \subscriberACDw.exe
   \classes
        \com\org\SubscriberACD\
             \Subscriber.class
        \Subscriber.jar
   \logs

但是,这并没有解决问题。任何人都可以阐明这一点吗?

将您的 --Classpath 参数更改为:

--Classpath=E:\SubscriberACD\classes\your-jar-filename.jar 

您几乎肯定还有其他需要的 jar 文件,因此只需使用 ; 将它们附加到 --Classpath 的末尾即可。 (分号)定界符...

--Classpath=E:\SubscriberACD\classes\your-jar-filename.jar;e:\other-dir\classes\some-other.jar;etc...