Search code examples
javalda

Permission denied when writing a file with Java


I am trying to run the JGibbLDA code, and when I run the code using java -mx512M -cp bin:lib/args4j-2.0.6.jar jgibblda.LDA -est -alpha 0.5 -beta 0.1 -ntopics 100 -niters 1000 -savestep 100 -twords 20 -dfile /path/to/data/data.txt, I got an error messages:

Error while writing word map /wordmap.txt (Permission denied)
java.io.FileNotFoundException: /wordmap.txt (Permission denied),

which refers to the following code:

public boolean writeWordMap(String wordMapFile){
    try{
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(wordMapFile), "UTF-8"));

        //write number of words
        writer.write(word2id.size() + "\n");

        //write word to id
        Iterator<String> it = word2id.keySet().iterator();
        while (it.hasNext()){
            String key = it.next();
            Integer value = word2id.get(key);

            writer.write(key + " " + value + "\n");
        }

        writer.close();
        return true;
    }
    catch (Exception e){
        System.out.println("Error while writing word map " + e.getMessage());
        e.printStackTrace();
        return false;
    }


}
}

I am not familiar with Java but I need to run this piece of code. Could you please tell me what is the wrong with the code? Thank you in advance!


Solution

  • From the JGibbLDA site ...

    JGibbLDA also saves a file called wordmap.txt that contains the maps between words and word's IDs (integer).

    wordmap.txt is referenced in two files in the source code:

    LDACmdOption.java and Model.java

    The specific errors you mention do not relate to the reading of the wordmap.txt but most likely originate from Dictionary.java AND Estimator.java:

    src/jgibblda/Dictionary.java: public boolean writeWordMap(String wordMapFile)

    src/jgibblda/Estimator.java: trnModel.data.localDict.writeWordMap(option.dir + File.separator + option.wordMapFileName);

    More specifically it is using the root directory / and attempting to write to wordmap.txt to the root context. That won't work if you're a non-privileged user and NOT able to write to /. You could try to run the program as root but I DO NOT recommend that as it's an awful idea to break security. What I would recommend is setting:

    option.dir

    to a location you are able to write to and try running it again...

    From the manual on the site:

    -dir : The input training data directory

    So add that to your option list and try again. I hope that helps.