You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by rcbandit <pe...@gmail.com> on 2013/05/20 08:37:35 UTC
How to create Java desktop application based on embedded Apache
Felix
I want to create desktop Java application based on Apache Framework. I
created this main method:
import dx57.activator.Activator;
import java.util.ServiceLoader;
import java.util.HashMap;
import java.util.Map;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
/**
* This class provides a static {@code main()} method so that the bundle can
be
* run as a stand-alone host application. In such a scenario, the
application
* creates its own embedded OSGi framework instance and interacts with the
* internal extensions to providing drawing functionality. To successfully
* launch the stand-alone application, it must be run from this bundle's
* installation directory using "{@code java -jar}". The locations of any
* additional extensions that have to be started, have to be passed as
command
* line arguments to this method.
*/
public class StartApplication
{
private static Framework m_framework = null;
public static Map<Integer, String> map = new HashMap<Integer, String>();
/**
* Enables the bundle to run as a stand-alone application. When this
static
* {@code main()} method is invoked, the application creates its own
* embedded OSGi framework instance and interacts with the internal
* extensions to provide drawing functionality. To successfully launch
as a
* stand-alone application, this method should be invoked from the
bundle's
* installation directory using "{@code java -jar}". The location of any
* extension that shall be installed can be passed as parameters.
* <p>
* For example if you build the bundles inside your workspace, maven
will
* create a target directory in every project. To start the application
from
* within your IDE you should pass:
* <p>
*
* <pre>
* {@code
file:../servicebased.circle/target/servicebased.circle-1.0.0.jar
* file:../servicebased.square/target/servicebased.square-1.0.0.jar
* file:../servicebased.triangle/target/servicebased.triangle-1.0.0.jar}
* </pre>
*
* @param args The locations of additional bundles to start.
*
*/
public static void main(String[] args)
{
map.put(1, "/opt/bundle1.jar");
// map.put(2, "test value 2");
// map.put(3, "test value 3");
// Args should never be null if the application is run from the
command
// line.
// Check it anyway.
//String[] locations = args != null ? args : new String[0];
// Print welcome banner.
System.out.println("\nWelcome to My Launcher");
System.out.println("======================\n");
try
{
Map<String, String> config = ConfigUtil.createConfig();
m_framework = createFramework(config);
m_framework.init();
m_framework.start();
installAndStartBundles();
m_framework.waitForStop(0);
System.exit(0);
}
catch (Exception ex)
{
System.err.println("Could not create framework: " + ex);
ex.printStackTrace();
System.exit(-1);
}
}
/**
* Util method for creating an embedded Framework. Tries to create a
* {@link FrameworkFactory} which is then be used to create the
framework.
*
* @param config the configuration to create the framework with
* @return a Framework with the given configuration
*/
private static Framework createFramework(Map<String, String> config)
{
ServiceLoader<FrameworkFactory> factoryLoader = ServiceLoader
.load(FrameworkFactory.class);
for (FrameworkFactory factory : factoryLoader)
{
return factory.newFramework(config);
}
throw new IllegalStateException(
"Unable to load FrameworkFactory service.");
}
/**
* Installs and starts all bundles used by the application. Therefore
the
* host bundle will be started. The locations of extensions for the host
* bundle can be passed in as parameters.
*
* @param bundleLocations the locations where extension for the host
bundle
* are located. Must not be {@code null}!
* @throws BundleException if something went wrong while installing or
* starting the bundles.
*/
private static void installAndStartBundles() throws BundleException,
Exception
{
BundleContext bundleContext = m_framework.getBundleContext();
Activator hostActivator = new Activator();
hostActivator.start(bundleContext);
for (String location : map.values())
{
Bundle addition = bundleContext.installBundle(location);
addition.start();
}
}
}
This is the POM file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>DX-57</groupId>
<artifactId>DX-57</artifactId>
<version>1.0</version>
<packaging>bundle</packaging>
<name>DX-57 OSGi Bundle</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>org.osgi.framework,
org.osgi.util.tracker, org.osgi.framework.launch,
org.osgi.framework.launch.FrameworkFactory</Import-Package>
<Bundle-Activator>dx57.activator.Activator</Bundle-Activator>
<Export-Package/>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>dx57.main.StartApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>build-for-felix</id>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.main</artifactId>
<version>4.2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>compile</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<pathconvert property="plugins.jars"
pathsep="${path.separator}">
<path
refid="maven.runtime.classpath"/>
<map
from="${project.build.directory}${file.separator}classes" to=""/>
</pathconvert>
<pathconvert pathsep=" "
property="bundles">
<path path="${plugins.jars}"/>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*"
to="file:modules/*" casesensitive="no"/>
</chainedmapper>
</mapper>
</pathconvert>
<propertyfile
file="${project.build.directory}/config.properties">
<entry key="felix.auto.start"
value="${bundles} file:modules/${project.build.finalName}.jar"/>
<entry
key="org.osgi.framework.bootdelegation" value="*"/>
</propertyfile>
<copy
file="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}"
tofile="${project.build.directory}/felix.jar"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>create-executable-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${basedir}/src/main/assembly/felix.xml</descriptor>
</descriptors>
<finalName>${project.build.finalName}</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>run-on-felix</id>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.main</artifactId>
<version>4.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<configuration>
<target>
<property name="vm.args" value=""/>
<pathconvert property="plugins.jars"
pathsep="${path.separator}">
<path refid="maven.runtime.classpath"/>
<map
from="${project.build.directory}${file.separator}classes" to=""/>
</pathconvert>
<makeurl property="urls" separator=" ">
<path path="${plugins.jars}"/>
<path
location="${project.build.directory}/${project.build.finalName}.jar"/>
</makeurl>
<propertyfile
file="${project.build.directory}/run.properties">
<entry key="felix.auto.start"
value="${urls}"/>
<entry key="felix.auto.deploy.action"
value="uninstall,install,update,start"/>
<entry key="org.osgi.framework.storage"
value="${project.build.directory}${file.separator}felix-cache"/>
<entry
key="org.osgi.framework.bootdelegation" value="*"/>
</propertyfile>
<makeurl property="run.properties.url"
file="${project.build.directory}/run.properties"/>
<java fork="true"
jar="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}">
<sysproperty
key="felix.config.properties" value="${run.properties.url}"/>
<jvmarg line="${vm.args}"/>
</java>
</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
When I start the bundle I get this error:
[rcbandit@Laptop target]$ java -jar ./DX-57-1.0.jar
Welcome to My Launcher
======================
Exception in thread "main" java.lang.NoClassDefFoundError:
org/osgi/framework/launch/FrameworkFactory
at dx57.main.StartApplication.createFramework(StartApplication.java:95)
at dx57.main.StartApplication.main(StartApplication.java:71)
Caused by: java.lang.ClassNotFoundException:
org.osgi.framework.launch.FrameworkFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 2 more
Can you help me to solve this problem, please?
--
View this message in context: http://apache-felix.18485.x6.nabble.com/How-to-create-Java-desktop-application-based-on-embedded-Apache-Felix-tp5003371.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: How to create Java desktop application based on embedded
Apache Felix
Posted by Neil Bartlett <nj...@gmail.com>.
I answered your question on StackOverflow already, so I'm not sure why you need to repeat it here. Anyway: you need to put the Felix framework JAR on the application class path.
Regards,
Neil.
--
Neil Bartlett
Sent from a phone
On Monday, 20 May 2013 at 07:37, rcbandit wrote:
> I want to create desktop Java application based on Apache Framework. I
> created this main method:
>
> import dx57.activator.Activator;
> import java.util.ServiceLoader;
> import java.util.HashMap;
> import java.util.Map;
> import org.osgi.framework.Bundle;
> import org.osgi.framework.BundleContext;
> import org.osgi.framework.BundleException;
> import org.osgi.framework.launch.Framework;
> import org.osgi.framework.launch.FrameworkFactory;
>
> /**
> * This class provides a static {@code main()} method so that the bundle can
> be
> * run as a stand-alone host application. In such a scenario, the
> application
> * creates its own embedded OSGi framework instance and interacts with the
> * internal extensions to providing drawing functionality. To successfully
> * launch the stand-alone application, it must be run from this bundle's
> * installation directory using "{@code java -jar}". The locations of any
> * additional extensions that have to be started, have to be passed as
> command
> * line arguments to this method.
> */
> public class StartApplication
> {
>
> private static Framework m_framework = null;
> public static Map<Integer, String> map = new HashMap<Integer, String>();
>
> /**
> * Enables the bundle to run as a stand-alone application. When this
> static
> * {@code main()} method is invoked, the application creates its own
> * embedded OSGi framework instance and interacts with the internal
> * extensions to provide drawing functionality. To successfully launch
> as a
> * stand-alone application, this method should be invoked from the
> bundle's
> * installation directory using "{@code java -jar}". The location of any
> * extension that shall be installed can be passed as parameters.
> * <p>
> * For example if you build the bundles inside your workspace, maven
> will
> * create a target directory in every project. To start the application
> from
> * within your IDE you should pass:
> * <p>
> *
> * <pre>
> * {@code
> file:../servicebased.circle/target/servicebased.circle-1.0.0.jar
> * file:../servicebased.square/target/servicebased.square-1.0.0.jar
> * file:../servicebased.triangle/target/servicebased.triangle-1.0.0.jar}
> * </pre>
> *
> * @param args The locations of additional bundles to start.
> *
> */
> public static void main(String[] args)
> {
>
> map.put(1, "/opt/bundle1.jar");
> // map.put(2, "test value 2");
> // map.put(3, "test value 3");
>
> // Args should never be null if the application is run from the
> command
> // line.
> // Check it anyway.
> //String[] locations = args != null ? args : new String[0];
>
> // Print welcome banner.
> System.out.println("\nWelcome to My Launcher");
> System.out.println("======================\n");
>
> try
> {
> Map<String, String> config = ConfigUtil.createConfig();
> m_framework = createFramework(config);
> m_framework.init();
> m_framework.start();
> installAndStartBundles();
> m_framework.waitForStop(0);
> System.exit(0);
> }
> catch (Exception ex)
> {
> System.err.println("Could not create framework: " + ex);
> ex.printStackTrace();
> System.exit(-1);
> }
> }
>
> /**
> * Util method for creating an embedded Framework. Tries to create a
> * {@link FrameworkFactory} which is then be used to create the
> framework.
> *
> * @param config the configuration to create the framework with
> * @return a Framework with the given configuration
> */
> private static Framework createFramework(Map<String, String> config)
> {
> ServiceLoader<FrameworkFactory> factoryLoader = ServiceLoader
> .load(FrameworkFactory.class);
> for (FrameworkFactory factory : factoryLoader)
> {
> return factory.newFramework(config);
> }
> throw new IllegalStateException(
> "Unable to load FrameworkFactory service.");
> }
>
> /**
> * Installs and starts all bundles used by the application. Therefore
> the
> * host bundle will be started. The locations of extensions for the host
> * bundle can be passed in as parameters.
> *
> * @param bundleLocations the locations where extension for the host
> bundle
> * are located. Must not be {@code null}!
> * @throws BundleException if something went wrong while installing or
> * starting the bundles.
> */
> private static void installAndStartBundles() throws BundleException,
> Exception
> {
> BundleContext bundleContext = m_framework.getBundleContext();
> Activator hostActivator = new Activator();
> hostActivator.start(bundleContext);
>
> for (String location : map.values())
> {
> Bundle addition = bundleContext.installBundle(location);
> addition.start();
> }
> }
> }
>
>
> This is the POM file:
>
> <project xmlns="http://maven.apache.org/POM/4.0.0"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
> http://maven.apache.org/xsd/maven-4.0.0.xsd">
> <modelVersion>4.0.0</modelVersion>
>
> <groupId>DX-57</groupId>
> <artifactId>DX-57</artifactId>
> <version>1.0</version>
> <packaging>bundle</packaging>
>
> <name>DX-57 OSGi Bundle</name>
>
> <properties>
> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
> </properties>
>
> <dependencies>
> <dependency>
> <groupId>org.osgi</groupId>
> <artifactId>org.osgi.core</artifactId>
> <version>5.0.0</version>
> </dependency>
> </dependencies>
>
> <build>
> <plugins>
> <plugin>
> <groupId>org.apache.felix</groupId>
> <artifactId>maven-bundle-plugin</artifactId>
> <version>2.3.7</version>
> <extensions>true</extensions>
> <configuration>
> <instructions>
> <Import-Package>org.osgi.framework,
> org.osgi.util.tracker, org.osgi.framework.launch,
> org.osgi.framework.launch.FrameworkFactory</Import-Package>
> <Bundle-Activator>dx57.activator.Activator</Bundle-Activator>
> <Export-Package/>
> </instructions>
> </configuration>
> </plugin>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-jar-plugin</artifactId>
> <version>2.4</version>
> <configuration>
> <archive>
> <manifest>
> <addClasspath>true</addClasspath>
> <mainClass>dx57.main.StartApplication</mainClass>
> </manifest>
> </archive>
> </configuration>
> </plugin>
> </plugins>
> </build>
>
> <profiles>
> <profile>
> <id>build-for-felix</id>
> <dependencies>
> <dependency>
> <groupId>org.apache.felix</groupId>
> <artifactId>org.apache.felix.main</artifactId>
> <version>4.2.1</version>
> </dependency>
> </dependencies>
> <build>
> <plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-antrun-plugin</artifactId>
> <version>1.7</version>
> <executions>
> <execution>
> <id>compile</id>
> <phase>package</phase>
> <goals>
> <goal>run</goal>
> </goals>
> <configuration>
> <target>
> <pathconvert property="plugins.jars"
> pathsep="${path.separator}">
> <path
> refid="maven.runtime.classpath"/>
> <map
> from="${project.build.directory}${file.separator}classes" to=""/>
> </pathconvert>
> <pathconvert pathsep=" "
> property="bundles">
> <path path="${plugins.jars}"/>
> <mapper>
> <chainedmapper>
> <flattenmapper/>
> <globmapper from="*"
> to="file:modules/*" casesensitive="no"/>
> </chainedmapper>
> </mapper>
> </pathconvert>
> <propertyfile
> file="${project.build.directory}/config.properties">
> <entry key="felix.auto.start"
> value="${bundles} file:modules/${project.build.finalName}.jar"/>
> <entry
> key="org.osgi.framework.bootdelegation" value="*"/>
> </propertyfile>
> <copy
> file="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}"
> tofile="${project.build.directory}/felix.jar"/>
> </target>
> </configuration>
> </execution>
> </executions>
> </plugin>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-assembly-plugin</artifactId>
> <version>2.4</version>
> <executions>
> <execution>
> <id>create-executable-jar</id>
> <phase>package</phase>
> <goals>
> <goal>single</goal>
> </goals>
> <configuration>
> <descriptors>
> <descriptor>${basedir}/src/main/assembly/felix.xml</descriptor>
> </descriptors>
> <finalName>${project.build.finalName}</finalName>
> </configuration>
> </execution>
> </executions>
> </plugin>
> </plugins>
> </build>
> </profile>
> <profile>
> <id>run-on-felix</id>
> <dependencies>
> <dependency>
> <groupId>org.apache.felix</groupId>
> <artifactId>org.apache.felix.main</artifactId>
> <version>4.2.1</version>
> <scope>provided</scope>
> </dependency>
> </dependencies>
> <build>
> <plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-antrun-plugin</artifactId>
> <version>1.7</version>
> <configuration>
> <target>
> <property name="vm.args" value=""/>
> <pathconvert property="plugins.jars"
> pathsep="${path.separator}">
> <path refid="maven.runtime.classpath"/>
> <map
> from="${project.build.directory}${file.separator}classes" to=""/>
> </pathconvert>
> <makeurl property="urls" separator=" ">
> <path path="${plugins.jars}"/>
> <path
> location="${project.build.directory}/${project.build.finalName}.jar"/>
> </makeurl>
> <propertyfile
> file="${project.build.directory}/run.properties">
> <entry key="felix.auto.start"
> value="${urls}"/>
> <entry key="felix.auto.deploy.action"
> value="uninstall,install,update,start"/>
> <entry key="org.osgi.framework.storage"
> value="${project.build.directory}${file.separator}felix-cache"/>
> <entry
> key="org.osgi.framework.bootdelegation" value="*"/>
> </propertyfile>
> <makeurl property="run.properties.url"
> file="${project.build.directory}/run.properties"/>
> <java fork="true"
> jar="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}">
> <sysproperty
> key="felix.config.properties" value="${run.properties.url}"/>
> <jvmarg line="${vm.args}"/>
> </java>
> </target>
> </configuration>
> </plugin>
> </plugins>
> </build>
> </profile>
> </profiles>
> </project>
>
>
> When I start the bundle I get this error:
>
> [rcbandit@Laptop target]$ java -jar ./DX-57-1.0.jar
>
> Welcome to My Launcher
> ======================
>
> Exception in thread "main" java.lang.NoClassDefFoundError:
> org/osgi/framework/launch/FrameworkFactory
> at dx57.main.StartApplication.createFramework(StartApplication.java:95)
> at dx57.main.StartApplication.main(StartApplication.java:71)
> Caused by: java.lang.ClassNotFoundException:
> org.osgi.framework.launch.FrameworkFactory
> at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
> ... 2 more
>
> Can you help me to solve this problem, please?
>
>
>
>
> --
> View this message in context: http://apache-felix.18485.x6.nabble.com/How-to-create-Java-desktop-application-based-on-embedded-Apache-Felix-tp5003371.html
> Sent from the Apache Felix - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>