简体   繁体   中英

java.io.FileNotFoundException: config.properties

I have a certain requirement where I need to copy files from Unix server to Windows Shared Drive. I am developing the necessary code for this in Java. I am a beginner so please excuse me for this basic question.

I have my source path in my config file. So, I am using the below code to import my config file and set my variable. My Project has config.properties file attached to it.

public static String rootFolder = "";
Properties prop = new Properties();
InputStream input = null;
    try {
        input = new FileInputStream("config.properties");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("Config files not able to set properly for Dest Folder");
    }   

  try {
        prop.load(input);
        rootFolder = prop.getProperty("Dest_Root_Path");
        System.out.println("Destination Folder is being initialized to - "+rootFolder);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Destination Path not set properly");
    }

When I am doing this I am getting an error saying the file is not found.

java.io.FileNotFoundException: config.properties (No such file or directory)
at java.io.FileInputStream.<init>(FileInputStream.java:158)
at java.io.FileInputStream.<init>(FileInputStream.java:113)

Exception in thread "main" java.lang.NullPointerException
at java.util.Properties.load(Properties.java:357)

I am triggering this jar using a unix ksh shell. Please provide guidance to me.

Put your config file somewhere within the classpath. For example, if it's a webapp, in WEB-INF/classes. If it isn't a webapp, create a folder outside the project, put the file there, and set the classpath so the new folder is in it.

Once you have your file in the classpath, get it as a resource with getResourceAsStream():

InputStream is = MyProject.class.getResourceAsStream("/config.properties");

Don't forget the slash / before the filename.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM