How to create an executable version of my OSGi project? - java

Is it possible to save my OSGi project (which comprises of few bundles) to a single executable file, something like an .exe file , so I can copy and run it to any PC that has a JVM.
I know that the normal method is to open a command prompt and install the required bundles/ jar files one by one. But since my project contains quite a few bundles that method seems tedious.
Thanx in Advance.

You can definitely do better than installing bundles one by one, even if you don't get as far as a single 'natively executable' archive like a .exe. There are lots of commercial and open source OSGi applications which ship as zip archives; the user unzips the archive and then either calls java -jar some.osgi.jar or runs a shell script.
There are a few ways the OSGi runtime can work out what bundles to install. Some are specific to an OSGi framework (such as Equinox or Felix) and others are more generic. If you're using Equinox, you can create a config.ini file and put it in a folder called configuration as the same level as your OSGi jar. List any bundles you want to start in the osgi.bundles property. The config.ini file can list all the bundles to start, and also any other configuration properties you might need.
Eclipse also allows you to define a minimal set of bundles in config.ini and use the configurator to start everything in the plugins folder. Similarly, if you're using Felix, any bundles in the auto-deploy directory will be automatically started. You could also look at Felix File Install, which allows you to drop bundles into a monitored folder to install them (once FileInstall itself is installed). Despite the name, FileInstall works on both Equinox and Felix.

This is similar to creating a complete OSGi application with Felix & Maven - the Sling Launchpad Plugin can be used to create an executable jar or war file that contains a specific set of bundles. If you're not using Maven yet it's not as simple as "saving your project to an executable file" but you could probably adapt the jar file generation mechanisms to your environment.

Eclipse has the notion of products. You could create a product from your existing bundles, and then your product can be exported as an executable. For details about the product see the Eclipse RCP tutorial from http://www.vogella.com/articles/EclipseRCP/article.html
The tutorial introduces the Eclipse RCP applications, but you do not need everything from there - it is possible to create a product without creating an entire Eclipse application.
This limits your choices as you are more or less bound to Eclipse Equinox as your OSGi runtime, but for creating an executable product from it might be a workable tradeoff.

Thanx.
Following the answers above I came across Chapter 9 in this book.
http://my.safaribooksonline.com/9780321561510/ch09#ch09
It had a walk through tutorial in exporting the project through the product configuration.
Its exactly what I needed. :)

If you use bnd(tools) then you can export a project to an executable jar either from the bndtools GUI or via the bnd commandline (bnd package xyz.bndrun; java -jar xyz.jar). The resulting jar file contains all the bundles, the launcher, and the framework.

Related

Where to place java packages like htmlparser?

I am beginning to use java packages like HTMLParser, I have downloaded it and finding that there are many files and directories in it.
I wander, where to place them in my linux system? Is there a convention or a standard?
The quick and dirty answer is "anywhere on the classpath", where the classpath is set either as a system property on the client machine (not recommended), as a temporary system property for the CLI session used to start the JVM (workable from a startup script), or as a commandline parameter to the JVM (usually the preferred choice).
First and second set the CLASSPATH environment variable, see the JDK or JRE documentation for the exact syntax and your operating system's and/or shell scripting documentation as well. Third uses the -cp commandline variable to the Java runtime and compiler, see their documentation for exact syntax.
Where to place the files on the filesystem? For development purposes I typically use a central folder on my computer containing all such libraries and link to that from my IDE or other development environment. For deployment/packaging to end users, it is traditional to have a "lib" subfolder to the product folder that contains all distributable content, and put the jar files in that.
Java packages come in two forms. Source code - all the files and directories you mention - and packaged as jars. A common convention in Java projects is that the project has a lib directory that contains all the jars that the project depends on. These projects often use a shell script which adds all the jars to the Java classpath prior to executing the project code.
However many projects are switching from this method of dealing with dependencies to using a build tool like Apache Maven which automatically handles dependency management. Other alternatives include Ivy or Gradle. For an introduction see the 5 minute introduction to Maven or the Maven 3 tutorial.
Here you write a pom.xml (project object model file) which specifies which libraries (jars) your project uses. Maven then stores all the jars for your different projects in a .m2 directory in your local directory, keeping track of where it obtained them, and their versioning information.
This makes developing much easier as you do not need to create the lib directory or manually manage dependencies. You also avoid a lot of the complexities of setting the classpath, as Maven automatically does this for you during common lifecyle stages such as compilation and test. Recent versions of Eclipse can read the Maven pom and automatically configure your classpath from it.
Once you have built the project, Maven can also help create "fat jars" that contain all the jars your project depends on, via the assembly plugin or the Shade plugin. This makes distributing the code easier when you are building an executable that you want someone to use. If you are distributing a jar, then your pom.xml describes the dependencies of your project, avoiding the need to distribute the jars it depends on.
For laying out files in general on a Linux system consult the Linux Filesystem hierarchy standard.

Netbeans generating .jar file

When I clean and build, Netbeans generates a .jar file for my Java project, and it uses a "lib" folder which has all of my external Jar files that I use. Can I somehow tell Netbeans to package these Jar files into the .jar it makes? So that I can run my project as a stand-alone .jar file?
I am using Netbeans 7.1.1 on Mac OSX
The short answer is no, the long answer is complicated.
Firstly, Java does not support embedded Jars (ie, you cann't simply add the depended Jars into the main Jar).
Secondly, uncompressing all the Jars and merging them into one will overwrite any resources/classes that share the same path/name. This may not be an issue in small projects but is a major issue in large ones (we have this problem with the project I'm working on at work).
One solution is to use something like One-Jar, which basically uses it's own boot class loader to all you to reference Jars inbedded within a single Jar.
I've had issues with this when using our own custom classloader, but otherwise it seems to work well.
It includes Ant support, so you can include it in your projects Ant build script, if you not using Maven, otherwise, you'll need to devise your own build process
There is no options in netbeans to do that.
There are some other options that a quick search would help, but requires manual intervention.

Standalone java class talking to a web application

i'm working on a quite complex java web application based on Spring framework and hibernate,
i have to create a normal java class (with main method) and export it in a .jar file; this class contains a procedure that will be manually schedulated monthly by an operator.
the results of the scheduling will be consulted on the web app pages (JSP).
i need to use the jar file like this : java -jar myfile.jar arguments ....
is there a way to do this ?
how to export all the dependencies derived from spring and hibernate ?
P.S. i tried export the jar file with MyEclipse but when i try to run the jar, it can't find spring classes
First of all the title is wrong, you need a standalone application.
Secondly, Every decent java IDE like should provide the option of exporting a jar. For example, in eclipse:
Right click the project
Choose export
Choose Runnable jar file
Another option is to use build tools like ant or maven.
Packaging dependencies is a topic which has no strict connection with Hibernate, Spring, Guice, or a Java library for generating random names for dogs.
The first thing you need is a build system, which I hope you have if
you are dealing with a quite complex java web application, and you
are not letting your ide build.
Once you have a build system such as Maven working correctly, you can
look to the packaging extensions. In Maven, this is called assembly
plugin.

package OSGI project as a single jar?

I have a large OSGI package that I want to package and release as a single jar file. I'm trying to figure out what the best approach is for packaging multiple jars into a single large jar.
So far the best option I've seen appears to be one-jar project. However, the framework we are using pulls in jar files from a 'plugins' directory and one-jar appears to want/require all jar files to be stored only in the lib directory. There may be an easy way around this, I haven't looked fully into the architecture enough to know as I'm still trying to decide what approach is best.
any suggestions for other approaches to package the multiple OSGI bundles into one jar and/or how I would go about making it work in one-jar is appreciated.
Thanks
An alternative solution could be to run your bundles using PojoSR instead of running them in an OSGi framework. PojoSR in a nutshell implements the service layer of OSGi without the module layer. One of the side effects of that is that you can easily package your application as an executable JAR file. When you run that JAR it also does not need to create a bundle cache on disk.
For more information on PojoSR, go to:
http://code.google.com/p/pojosr/
http://luminis-technologies.com/?p=358
http://www.infoq.com/news/2011/10/pojosr
Using the Apache Felix Maven Bundle Plugin, I believe the option might accomplish what you are looking for. http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html
Apache Sling's maven-launchpad-plugin generates a runnable jar (and optionally a war file and a Karaf descriptor) from a list of bundles, see http://sling.apache.org/site/maven-launchpad-plugin.html
The Sling installer can be used to load additional bundles from the filesystem or other sources, see http://sling.apache.org/site/jcr-installer-jcrjcrinstall-and-osgiinstaller.html

Can OpenCms be integrated, or used, with an IDE?

I'm starting with a new project where we'll be using OpenCms. Can the workplace be integrated to an IDE, and which one is it ?
Also if you are planning to use Eclipse OpenCms module with OpenCms 8, I am afraid you have to use the WebDAV Eclipse plugin with OpenCms, the other plugin stop working.
Link to the wiki reference page.
So once you may set:
Create a blank new module on your OpenCms.
Create a standard Java project.
Change the project setup to create the class files under a folder called "classes" instead of bin.
Import the module through WebDAV into the new module.
Create an user library with all the jars placed under the lib/ folder located in the OpenCms deployed war and add it to the buildpath.
Now you are ready to go.
If you're just going to develop on your local machine, I'd recommend the OpenCms Module Developer for Eclipse. The only thing you have to be aware of, is setting the encoding of the files to the same (by default Eclipse has some ISO-9... for editors which can be changed in the preferences).
Otherwise you could utilize the webdav-access to the VFS to edit with your favorite Editor.
The OpenCms Wiki might help as well in general.
This should help if you use Maven (which you can use with Eclipse):
Fully automated builds with the OpenCms-Maven-Plugin:
OpenCms-Maven is an open source
project ... to ease the build and
versioning process of OpenCms
projects. The core of the project is a
Maven plugin that enables full
automated OpenCms builds and provides
functionality to synchronize virtual
file system resources of OpenCms with
a real file system.
here is the link on the wiki that works...
http://www.opencms-wiki.org/wiki/Main_Page

Categories