Search code examples
javajarenvironment-variableswordnetjwi

how to set environment variable java unix .getenv(NOT_WORKING)


printenv WNHOME echo $WNHOME both give me the correct answer but the java program does not.

I'm trying to execute the example program featured in the JWI (the MIT Java Wordnet Interface), the first one featured in the 'User's Manual', as expressed in this question. However, when running the code I keep getting the error java.net.MalformedURLException. Here is a discussion which seems to be dealing with a similar issue however I've attempted the solutions therein proposed to no avail.

The code looks like this:

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.net.URL;
import java.nio.file.Paths;

import edu.mit.jwi.*;
import edu.mit.jwi.item.IIndexWord;
import edu.mit.jwi.item.ILexFile;
import edu.mit.jwi.item.ISenseKey;
import edu.mit.jwi.item.IWord;
import edu.mit.jwi.item.IWordID;
import edu.mit.jwi.item.POS;

public class MITJavaWordNetInterface 
{
public static void main(String[] args) throws IOException
{
    // construct the URL to the Wordnet dictionary directory
    String wnhome = System.getenv("WNHOME");
    String path = wnhome + File.separator + "dict";
    System.out.println("Path is '" + path + "'"); 
    URL url = new URL ("file", null , path );
    //final URL url = Paths.get(wnhome, "dict").toUri().toURL();

    // construct the dictionary object and open it
    IDictionary dict = new Dictionary ( url ) ;
    dict . open () ;

    // look up first sense of the word "dog "
    IIndexWord idxWord = dict . getIndexWord ("dog", POS . NOUN ) ;
    IWordID wordID = idxWord . getWordIDs () . get (0) ;
    IWord word = dict . getWord ( wordID ) ;
    System . out . println ("Id = " + wordID ) ;
    System . out . println (" Lemma = " + word . getLemma () ) ;
    System . out . println (" Gloss = " + word . getSynset () . getGloss () ) ;      
}       
}

There was a prerequisite step of setting the system environment variable WNHOME to the location of the root of my Wordnet istallation, which I've duly completed. My WNHOME variable is /usr/local/WordNet-3.0. What else could be the cause of this error? How can it be resolved?

I tried changing the URL to the following (equally ineffective).

final URL url = Paths.get(wnhome, "dict").toUri().toURL();

The results of System.out.println("Path is '" + path + "'"); is:

Path is 'null/dict'

The full error is this:

Exception in thread "main" java.io.IOException: Dictionary directory does not exist: null/dict
    at edu.mit.jwi.data.FileProvider.open(FileProvider.java:306)
    at edu.mit.jwi.DataSourceDictionary.open(DataSourceDictionary.java:92)
    at edu.mit.jwi.CachingDictionary.open(CachingDictionary.java:133)
    at MITJavaWordNetInterface.main(MITJavaWordNetInterface.java:28)

Solution

  • You seem to have copied-and-pasted this code from somewhere.

    The code as you are trying to run it has lots of extraneous spaces. Either they were inserted by whatever you used to copy-and-paste, or they were added by the compositing software.

    Some of the extra spaces are harmless, but there are (at least) 3 places where they are harmful:

    String wnhome = System.getenv(" WNHOME ");
    String path = wnhome + File.separator + " dict ";
    URL url = new URL (" file ", null , path ) ;
    

    Remove the extraneous spaces in the 3 string literals, and the code is likely to behave (more or less) correctly.


    Lesson: it is a bad idea to copy code from textbooks ... when you don't understand what the code is doing and/or the programming language it is written in.


    If that doesn't fix the problem, then print out what the path component are:

    System.out.println("Path is '" + path + "'"); 
    

    and show us the complete stacktrace and the complete exception message.


    the result was Path is 'null/dict'

    That means that wnhome is null, which means that either you have not set the environment variable correctly, or the environment variable name you are looking up is incorrect.