Problem investigating JmDNS3.4.1 jar
I'm newbie here. I'm dealing with my first program using osgi bundles and JmDNS. After adding JmDNS 3.4.1 jar to my project, I'm testing the following basic code:
My Activator:
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");
}
}
and here is the bundle:
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("Servic开发者_如何学Goe removed : " + event.getName() + "." + event.getType());
}
@Override
public void serviceResolved(ServiceEvent event) {
System.out.println("Service resolved: " + event.getInfo());
}
}
}
when I run the code, I get :
osgi> Launching
step 1
and then it stops, so I guess there is a probelm with the creation of the JmDNS instance.. Any idea please?
Note that if I don't use a bundle with an activator (simple program with main) everything works properly
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: I'm running it on Windows
You have to add the imported library in the classpath of the MANIFEST.MF file of the OSGI Framework if not already done.
Bundle-ClassPath: org.json.jar,
lib/jmdns-3.4.1.jar, ...
If you got the lirary from sourceforge check if the jar file is correct and no class is present twice in the jar. (If so, just use the .jar from Maven)
精彩评论