在终端中运行时无法在java中加载外部文件

时间:2013-12-06 08:11:48

标签: java ubuntu terminal classpath config

我创建了一个可执行jar包(名为myjar.jar),它有一个用于读取配置文件的类(称为config_reader)。在myjar.jar之外还有另一个使用config_reader的类文件。但是config_reader的配置文件放在myjar.jar之外(在本地文件系统中)。现在,当我尝试执行可执行jar以及另一个使用它的文件时,我收到错误,说找不到配置文件:
我试过了 : java -classpath config myclass.class

任何人都可以帮忙吗?

config_reader的代码是:

public class config_reader()
{
     public static ArrayList<String> get_prop()
    {
        Properties prop = new Properties();
        ArrayList<String> s= new ArrayList<String>();
        try {
               //load a properties file

            prop.load(new FileInputStream("config"));

               //get the property value and print it out
               s.add(prop.getProperty("source_folder_dir"));
               s.add(prop.getProperty("dest_folder_dir"));
               s.add(prop.getProperty("file_type"));
               s.add(prop.getProperty("username"));
               s.add(prop.getProperty("userpwd"));
               s.add(prop.getProperty("exclusion_list"));


        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return s;
    }
}

1 个答案:

答案 0 :(得分:0)

prop.load(new FileInputStream("config"));

而不只是说config使用文件的绝对路径。事实上,最好做

    File configFile = new File("absolute path to config file");
    if(configFile.exists()){
        //continue with your logic
    }