Search code examples
javawebsphere-liberty

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?


Solution

  • 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