How to build a jar file for use in OSGi environment

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to build a jar file for use in OSGi environment

Robin Clark
I am trying to follow along with the examples in the book Enterprise OSGi In Action by Holly Cummins and Tim Ward.  I have successfully installed and have running the Apache Aries examples download (http://archive.apache.org/dist/aries/samples-1.0.0-source-release.zip).  I want to create a simple jar that contains one servlet class.  The directory structure contains two directories

1) META-INF which contains the MANIFEST.MF
2) WEB-INF which contains web.xml and the compiled simple servlet class

I am using Eclipse IDE for Java Developers Version 2019-03 (4.11.0).  I know that I need to first create a new project.  When I do File->New in the eclipse IDE, I am presented with many options.  I do not know which one to choose so that I can successfully write my tiny servlet class and build the jar.

[code=java]
package fancyfoods.web;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class SayHello extends HttpServlet {

protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.append("Hello valued customer!");
}

}
[/code]

I want the WEB-INF/web.xml to look like this:

[code=java]
<web-app>
<servlet>
<servlet-name>SayHello</servlet-name>
<servlet-class>fancyfoods.web.SayHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SayHello</servlet-name>
<url-pattern>SayHello</url-pattern>
</servlet-mapping>
</web-app>
[/code]

And I want the MANIFEST-MF file to look like this:

[code=java]
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: fancyfoods.web
Bundle-Version: 1.0.0
Bundle-ClassPath: WEB-INF/classes
Web-ContextPath: /fancyfoods.web
Bundle-Vendor: EXAMPLE
Automatic-Module-Name: fancyfoods
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: javax.servlet.http;version="[2.5,3.0)", javax.servlet;version="[2.5,3.0)"
[/code]

I chose the Java Plug-in project for File->New.  I have the following errors in my project that I do not know how to resolve.

[url=<a href="https://ibb.co/199kDM5]Image">https://ibb.co/199kDM5]Image showing error[/url]

Can anyone help me to understand how to create the simple jar and resolve these errors?  Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: How to build a jar file for use in OSGi environment

Grzegorz Grzybek
Hello

Please take a look at samples from pax-web projects: https://github.com/ops4j/org.ops4j.pax.web/tree/master/samples
All the projects there are bundles (war or jar) that can be installed in Apache Karaf or any OSGi runtime.

Your question is simply too wide (and I can't access the image) to answer without writing a book ;)

regards
Grzegorz Grzybek

sob., 8 cze 2019 o 21:41 Robin Clark <[hidden email]> napisał(a):
I am trying to follow along with the examples in the book Enterprise OSGi In Action by Holly Cummins and Tim Ward.  I have successfully installed and have running the Apache Aries examples download (http://archive.apache.org/dist/aries/samples-1.0.0-source-release.zip).  I want to create a simple jar that contains one servlet class.  The directory structure contains two directories

1) META-INF which contains the MANIFEST.MF
2) WEB-INF which contains web.xml and the compiled simple servlet class

I am using Eclipse IDE for Java Developers Version 2019-03 (4.11.0).  I know that I need to first create a new project.  When I do File->New in the eclipse IDE, I am presented with many options.  I do not know which one to choose so that I can successfully write my tiny servlet class and build the jar.

[code=java]
package fancyfoods.web;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class SayHello extends HttpServlet {

protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.append("Hello valued customer!");
}

}
[/code]

I want the WEB-INF/web.xml to look like this:

[code=java]
<web-app>
<servlet>
<servlet-name>SayHello</servlet-name>
<servlet-class>fancyfoods.web.SayHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SayHello</servlet-name>
<url-pattern>SayHello</url-pattern>
</servlet-mapping>
</web-app>
[/code]

And I want the MANIFEST-MF file to look like this:

[code=java]
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: fancyfoods.web
Bundle-Version: 1.0.0
Bundle-ClassPath: WEB-INF/classes
Web-ContextPath: /fancyfoods.web
Bundle-Vendor: EXAMPLE
Automatic-Module-Name: fancyfoods
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: javax.servlet.http;version="[2.5,3.0)", javax.servlet;version="[2.5,3.0)"
[/code]

I chose the Java Plug-in project for File->New.  I have the following errors in my project that I do not know how to resolve.

[url=https://ibb.co/199kDM5]Image showing error[/url]

Can anyone help me to understand how to create the simple jar and resolve these errors?  Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: How to build a jar file for use in OSGi environment

Aruna Karunarathna
Hi,

Is your project or jar crated using maven?  then you can try the maven-bundle-plugin [1]?


On Sun, Jun 9, 2019 at 11:33 AM Grzegorz Grzybek <[hidden email]> wrote:
Hello

Please take a look at samples from pax-web projects: https://github.com/ops4j/org.ops4j.pax.web/tree/master/samples
All the projects there are bundles (war or jar) that can be installed in Apache Karaf or any OSGi runtime.

Your question is simply too wide (and I can't access the image) to answer without writing a book ;)

regards
Grzegorz Grzybek

sob., 8 cze 2019 o 21:41 Robin Clark <[hidden email]> napisał(a):
I am trying to follow along with the examples in the book Enterprise OSGi In Action by Holly Cummins and Tim Ward.  I have successfully installed and have running the Apache Aries examples download (http://archive.apache.org/dist/aries/samples-1.0.0-source-release.zip).  I want to create a simple jar that contains one servlet class.  The directory structure contains two directories

1) META-INF which contains the MANIFEST.MF
2) WEB-INF which contains web.xml and the compiled simple servlet class

I am using Eclipse IDE for Java Developers Version 2019-03 (4.11.0).  I know that I need to first create a new project.  When I do File->New in the eclipse IDE, I am presented with many options.  I do not know which one to choose so that I can successfully write my tiny servlet class and build the jar.

[code=java]
package fancyfoods.web;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class SayHello extends HttpServlet {

protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.append("Hello valued customer!");
}

}
[/code]

I want the WEB-INF/web.xml to look like this:

[code=java]
<web-app>
<servlet>
<servlet-name>SayHello</servlet-name>
<servlet-class>fancyfoods.web.SayHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SayHello</servlet-name>
<url-pattern>SayHello</url-pattern>
</servlet-mapping>
</web-app>
[/code]

And I want the MANIFEST-MF file to look like this:

[code=java]
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: fancyfoods.web
Bundle-Version: 1.0.0
Bundle-ClassPath: WEB-INF/classes
Web-ContextPath: /fancyfoods.web
Bundle-Vendor: EXAMPLE
Automatic-Module-Name: fancyfoods
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: javax.servlet.http;version="[2.5,3.0)", javax.servlet;version="[2.5,3.0)"
[/code]

I chose the Java Plug-in project for File->New.  I have the following errors in my project that I do not know how to resolve.

[url=https://ibb.co/199kDM5]Image showing error[/url]

Can anyone help me to understand how to create the simple jar and resolve these errors?  Thank you.


--

Aruna Sujith Karunarathna
LinkedIn, Github, DZone
Mobile: +94 71 9040362