0

我是这里的新手。我正在使用 osgi 包和 JmDNS 处理我的第一个程序。将 JmDNS 3.4.1 jar 添加到我的项目后,我正在测试以下基本代码:

我的激活器:

package test.discoverservice;

import java.io.IOException;
import test.DiscoverServices;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceTypeListener;

import org.equinoxosgi.jmdns.dev.discoverservice.DiscoverServices.SampleListener;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class JmdnsActivator implements BundleActivator {

public void start(BundleContext context) throws Exception {
    System.out.println("Launching");
    try {
            System.out.println("step 1");
                final JmDNS jmdns = JmDNS.create();
        System.out.println("step 2");
                jmdns.addServiceListener("_http._tcp.local.", new SampleListener());              
               // jmdns.close();
               // System.out.println("Done");
             } catch (IOException e) {
                 e.printStackTrace();
             }  
}

public void stop(BundleContext context) throws Exception {
    System.out.println("Terminating");
}
}

这是捆绑包:

package test.discoverservice;

import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;


public class DiscoverServices {

   static class SampleListener implements ServiceListener {
       @Override
       public void serviceAdded(ServiceEvent event) {
           System.out.println("Service added   : " + event.getName() + "." +   event.getType());
        }

       @Override
       public void serviceRemoved(ServiceEvent event) {
           System.out.println("Service removed : " + event.getName() + "." + event.getType());
       }

       @Override
       public void serviceResolved(ServiceEvent event) {
           System.out.println("Service resolved: " + event.getInfo());
       }
   }    
  }

当我运行代码时,我得到:

osgi> Launching
step 1

然后它停止了,所以我猜JmDNS实例的创建有一个问题。请问有什么想法吗?

请注意,如果我不使用带有激活器的捆绑包(带有 main 的简单程序),一切正常

import java.io.IOException;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceTypeListener;
public class DiscoverServiceTypes {
    static class SampleListener implements ServiceTypeListener {
    @Override
    public void serviceTypeAdded(ServiceEvent event) {
        System.out.println("Service type added: " + event.getType());
    }
    public void subTypeForServiceTypeAdded(ServiceEvent event) {
        System.out.println("SubType for service type added: " + event.getType());
    }
}


public static void main(String[] args) {
    try {
        JmDNS jmdns = JmDNS.create();
        System.out.println("JmDNS created !!");
        jmdns.addServiceTypeListener(new SampleListener());
       // jmdns.close();
       // System.out.println("Done");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

PS:我在 Windows 上运行它

4

1 回答 1

0

如果尚未完成,您必须将导入的库添加到 OSGI 框架的 MANIFEST.MF 文件的类路径中。
Bundle-ClassPath: org.json.jar, lib/jmdns-3.4.1.jar, ...

如果您从 sourceforge 获得 lirary,请检查 jar 文件是否正确,并且 jar 中没有两次出现任何类。(如果是这样,只需使用来自 Maven 的 .jar)

于 2012-06-22T12:30:25.983 回答