-1

I have a gradle project with app.properties file present in the src/main/resources folder.

When i am trying to execute the project using jar, it says not able to read the app.properties file specified.

Below is the code to read the properties file :

  Properties properties = new Properties();  
  File file = new File("src/main/resources/app.properties");
  FileInputStream inputStream = new FileInputStream(file);
  properties.load(inputStream);

I am using below command to run the jar :

Java -jar App.jar

Any help on this issue will be appreciated.

Yathish Manjunath
  • 1,919
  • 1
  • 13
  • 23

3 Answers3

2

Try with getSystemResourceAsStream

 try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("app.properties"))
NSKBpro
  • 373
  • 1
  • 14
0

Most probably, when you make that project jar, the destination of the run file is changed. My English is not good, I know but I can suggest you that:

Make :

File file = new File("app.properties");

and please be sure that, App.jar and app.properties are in the same folder.

Have a good day :)

Another solution,

FileReader fileReader = new FileReader("app.properties");
 BufferedReader bufferedReader = new BufferedReader(fileReader);
 while((line = bufferedReader.readLine()) != null) {
        if(line.contains(/* WHAT YOU WANT*/)){
               // DO THE PROCESS    
         }  
  }

 bufferedReader.close();

You can try this too as an alternative.

Prodian
  • 71
  • 9
0

For someone still facing this problem:

public static void main(String[]  args){  
        InputStream is=MainMethod.class.getClassLoader().getResourceAsStream("xy.xml");
        Properties properties = new Properties();
        properties.loadFromXML(is);
        //xy.properties or xy.xml
        String somedata = properties.getProperty("somedata");
    }

xy.xml should be packaged along with the jar. Its working for me in both windows and lynux server. This works for normal java program as well.

Vikash Kumar
  • 1,096
  • 11
  • 10