Search code examples
javajakarta-eewordnet

How to use Wordnet in Java EE?


I have the following problem: I'm currently working on a Java EE project and I need to use the wordnet dictionary to search for synonyms of words.

I tested the basic wordnet functionalities with JWI 2.4 in another Java SE project. All what I need is working for me. But here comes the problem:

I want to load the wordnet dictionary into memory. Therefor I first exported the dictionary with JWI and put the exported file into the ressources folder of my java EE project. Then I tried to get the file with

InputStream in = this.getClass().getClassLoader().getResourceAsStream(name)

With this I then have an InputStream. But for creating an inmemory dictionary with JWI I need a file object or an url object. I tried to write the content of the inputstream into a temporary file but this is not allowed in java EE.

So here is my question. How can I integrate the wordnet dictionary into my java EE project?

Thanks for your help :) Appreciate that.


Solution

  • You can retrieve the URL of a Resource by using getResource. This URL may also point to a resource file within an JAR, WAR, or EAR.

     URL resourceUrl = this.getClass().getClassLoader().getResource(name);
    

    So if you put your file into your deployment artefact (jar, ear, war) you can access them, also in an JEE environment.

    If there is no additional restriction for the URL in your WorldNet library you can create the in memory dictionary.

    See Accessing Resources