0

I have a java project with multiple packages and classes. The main class which is called Driver.java is as follows:

package net.librec.tool.driver;
import net.librec.conf.Configuration;
import net.librec.job.RecommenderJob;
import net.librec.math.algorithm.Randoms;

import java.io.FileInputStream;
import java.util.Properties;

/**
 * Created by Himan on 12/5/2016.
 */
public class Driver {

    // Change this to load a different configuration file.
    public static String CONFIG_FILE =
            "conf/NBayes.properties";

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();

        String confFilePath = CONFIG_FILE;
        Properties prop = new Properties();
        prop.load(new FileInputStream(confFilePath));
        for (String name : prop.stringPropertyNames()) {
            conf.set(name, prop.getProperty(name));
        }

        Randoms.seed(20161205);
        RecommenderJob job = new RecommenderJob(conf);
        job.runJob();
        System.out.print("Finished");
    }
}

As you can see, in this class, I read the NBayes.properties. Here is the snapshot of my project structure:

Project Structure

I have created a jar file called librec.jar. However, when I run the program in command line using:

java -jar librec.jar 

I get the following error:

java.io.FileNotFoundException: conf/NBayes.properties (No such file or directory)

What is the problem and what is the right way to run this program? Was making a jar file necessary?

HimanAB
  • 2,443
  • 8
  • 29
  • 43
  • 1
    try loading file using getResourceAsStream(). – Atul Mar 12 '17 at 20:31
  • can you also check if your properties file is present in same directory or present insider jar file. – Atul Mar 12 '17 at 20:43
  • No it's not. How can I put it there? – HimanAB Mar 12 '17 at 20:47
  • If file is not present in the classpath, then the applicaiton will fail to load it. You need to put properties file either inside jar or within the same directory under conf directory. – Atul Mar 12 '17 at 20:49

1 Answers1

0

The current working directory when you execute the .jar file needs to contain the conf/ directory.

Evidently it doesn't. You need to reorganize your distribution so that it does. Or else if the properties files are read-only, put them inside the .jar file and access them as resources, via e.g. Class.getResourceAsStream().

user207421
  • 305,947
  • 44
  • 307
  • 483