Falco
Falco

Reputation: 1598

How to use shared library in websphere liberty profile

It seems simple but ...

I'm just starting to learn liberty profile.

I can't use shared library.

here's documentation: https://www.ibm.com/support/knowledgecenter/SSD28V_liberty/com.ibm.websphere.wlp.core.doc/ae/cwlp_sharedlibrary.html

so in server.xml I put (and restart server), for example:

<library>
    <folder dir="C:/libs/gson/"></folder>
    <!-- or even <file name="C:/libs/gson/gson-2.3.1.jar" /> -->
</library>

Anyway at runtime I receive: "java.lang.NoClassDefFoundError: com/google/gson/Gson"

On a servlet I just have the import and a simple use:

...    
import com.google.gson.Gson;
...

@WebServlet("/")
public class HelloWorld extends HttpServlet {
    ...

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // Serialization
        Gson gson = new Gson();
        ...

What I'm missing?

Upvotes: 3

Views: 2281

Answers (2)

Andy Guibert
Andy Guibert

Reputation: 42926

The part you are missing is that the application needs to be configured to reference the library. There are a few ways to do this:

  1. Use the Global shared library as described here: https://www.ibm.com/support/knowledgecenter/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/twlp_classloader_global_libs.html

Then, simply change your library ID to global like this:

<library id="global">
  1. Give your library an ID and add a <classloader commonLibraryRef="..."/> as described in @njr's answer

Upvotes: 6

njr
njr

Reputation: 3484

Adding a library element to server configuration doesn't automatically make it available to applications. You need to configure the application to have access to the library, for example,

<application location="myapp.war">
  <classloader commonLibraryRef="gsonLib"/>
</application>

<library id="gsonLib">
    <file name="C:/libs/gson/gson-2.3.1.jar" />
</library>

Upvotes: 8

Related Questions