2
votes

I'm having some problems following this tutorial (which was supposed to be quite easy):

http://www.javaworld.com/article/2077837/java-se/hello--osgi--part-1--bundles-for-beginners.html

At first the problem was the huge amount of errors whose source I couldn't find (but despite the errors, the output of the programme was correct). I read this about that problem:

This usually comes when someone tries to run a Java application against an OSGi bundle with java -classpath .... . It really means that the workbench plug-in hasn't started yet, and so calls to getWorkbench() fail. This is essentially a race condition, and can be solved by either expressing an explicit dependency on that bundle or bumping up that bundle to a higher start level than the workbench. Generally not seen, but if it is, that's what's happening.

I didn't understand the first solution the author presented, but I tried the second unsuccessfully. Then I reached the 5th (and last) page of the tutorial, and everything got worst. I tried to create the service factory as suggested by the author, and the outputs I was getting were not accurate. He states this:

Now try running this sample code. You should notice that it prints the service usage count as one (1) when HelloWorld bundle is started and zero (0) when the HelloWorld bundle is stopped.

But I was only getting the first output (usage counter printed as 1), and nothing else. While googling, I found this possible solution for the first problem, and I thought it might solve the second as well:

I struggled a lot for “Workbench has not been created yet” the complete day. But I got the solution by the following steps. Go to the Run configuration -> Remove all the target platform jars. Click on add required bundles. Check the org.apache.felix.gogo.runtime, org.apache.felix.gogo.shell, org.eclipse.eqinox.console, org.eclipse.osgi and your jar file. The last and important step. Go to Setting-> check the checkbox of "Clear the configuratation area before launching". Run the OSGi application now.

Enjoy if it works for you as it worked for me.

Does "removing all the target platform jars" mean that I should uncheck all the bundles in the Bundles tab in the Run Configurations? That's what I did, and it just got worse. By doing this, I was getting no output and some errors (not a bunch of them, like before), which I print below.

!ENTRY org.eclipse.osgi 4 0 2014-02-03 14:50:46.399 !MESSAGE Bundle reference:file:C:/Users/User/workspace/com.javaworld.sample.helloservice@5 not found.

!ENTRY org.eclipse.osgi 2 0 2014-02-03 14:50:46.473 !MESSAGE One or more bundles are not resolved because the following root constraints are not resolved: !SUBENTRY 1 org.eclipse.osgi 2 0 2014-02-03 14:50:46.474 !MESSAGE Bundle initial@reference:file:../../../workspace/com.javaworld.sample.helloworld/ was not resolved. !SUBENTRY 2 com.javaworld.sample.helloworld 2 0 2014-02-03 14:50:46.474 !MESSAGE Missing imported package com.javaworld.sample.service_0.0.0.

!ENTRY org.eclipse.osgi 2 0 2014-02-03 14:50:46.475 !MESSAGE The following is a complete list of bundles which are not resolved, see the prior log entry for the root cause if it exists: !SUBENTRY 1 org.eclipse.osgi 2 0 2014-02-03 14:50:46.475 !MESSAGE Bundle com.javaworld.sample.helloworld_1.0.0.qualifier [2] was not resolved. !SUBENTRY 2 com.javaworld.sample.helloworld 2 0 2014-02-03 14:50:46.475 !MESSAGE Missing imported package com.javaworld.sample.service_0.0.0.

Ok, I thought that just by selecting everything again (I think that it was the way it was before) I would be back on business, but now I'm getting no output at all. Any idea on what I'm doing wrong?

1
Which version of eclipse? I recall having similar problems with 4.2 initially. - Achim Schmitz
You might also be interested in my series of tutorials about OSGi. My approach is quite different. I show how to create bundles as regular maven projects. No plugin perspective required. liquid-reality.de/display/liquid/Karaf+Tutorials - Christian Schneider
Just wanted to say that working with OSGi bundles is really not that hard, what you're hitting into is the trickery of Eclipse. Eclipse has its own share issues and version-specific workarounds and I would personally recommend starting with an easier container that doesn't try to do as much as Eclipse tries. - eis
Thanks for your help! My Eclipse's version is this one: Version: Kepler Service Release 1 Build id: 20130919-0819 I can't find a way to further specify the version... - ccoutinho

1 Answers

2
votes

Does "removing all the target platform jars" mean that I should uncheck all the bundles in the Bundles tab in the Run Configurations? That's what I did, and it just got worse. By doing this, I was getting no output and some errors (not a bunch of them, like before), which I print below.

When you open your OSGI configuration (Bundles tab) your have possibility to include/exclude bundle from a) Workspace b) Target Platform

"removing all the target platform jars" means uncheck Target Platform node.

I believe the authors idea was ( haven't read the article) that you do the following 1) check Workspace bundles - check that all your necessary bundles are included 2) Uncheck Target platform bundles 3) Click button Add required bundles

With step 1) you include all of yours bundles. With step 2) and 3) include all required bundles are included.

If you want to use console make sure that -consoleLog -console is present on Arguments tab (under Program arguments) and that you have included org.apache.felix.gogo.runtime, org.apache.felix.gogo.shell, org.eclipse.eqinox.console.