0

I am trying to load a properties file into Java present on linux dir. connection.properties:

hiveDriver=HiveDriver
hiveServer=ip-1-2-1-1.
hivePort=123
hiveUser=huser
hivePassword=etl123
gpDriver=org.postgresql.Driver
metaStoreUrl=metaurl
port=5432
metaUser=devusr
metaPassword=abcdefg
gpAnalyticsServer=1.2.3.4.5
gpUser=gpuser
gpPassword=09987665

Code:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

try {
    Properties props = new Properties();
    String propFile  = "/home/devuser/connection.properties";
    InputStream inputStream = StartCount.class.getClassLoader().getResourceAsStream(propFile);
    if(inputStream != null) {
        props.load(inputStream);
    }
    String hiveDriver           = props.getProperty("hiveDriver");
    String hiveServer           = props.getProperty("hiveServer");
    String hivePort             = props.getProperty("hivePort");
    String hiveUser             = props.getProperty("hiveUser");
    String hivePassword         = props.getProperty("hivePassword");
    String gpDriver             = props.getProperty("gpDriver");
    String hiveMetaStoreServer  = props.getProperty("hiveMetaStoreServer");
    String port                 = props.getProperty("port");
    String hiveMetaUser         = props.getProperty("hiveMetaUser");
    String hiveMetaPassword     = props.getProperty("hiveMetaPassword");
    String gpAnalyticsServer    = props.getProperty("gpAnalyticsServer");
    String gpUser               = props.getProperty("gpUser");
    String gpPassword           = props.getProperty("gpPassword");
    System.out.println(hiveDriver)      ;
    System.out.println(hiveServer);
    System.out.println(hivePort);
    System.out.println(hiveUser);
    System.out.println(hivePassword);
    System.out.println(gpDriver);
    System.out.println(hiveMetaStoreServer);
    System.out.println(port);
    System.out.println(hiveMetaUser);
    System.out.println(hiveMetaPassword);
    System.out.println(gpAnalyticsServer);
    System.out.println(gpUser);
    System.out.println(gpPassword); 
} catch(Exception e) {
    e.printStackTrace();
}

I am submitting the jar from the same place where "connection.properties" is saved. When I run the code, I see null printing from the println statements. Could anyone let me know what is the mistake I did in the code above ?

2
  • 1
    getResourceAsStream looks for a resource in the classpath, is the parent directory of "/home/connection.properties" in the classpath ? Commented Mar 29, 2019 at 11:29
  • Its "/home/devuser/connection.properties". I had it corrected. I should've checked it while posting the question. Commented Mar 29, 2019 at 11:44

3 Answers 3

1
InputStream inputStream = new FileInputStream(propFile);

Thats it...

Because

StartCount.class.getClassLoader().getResourceAsStream(propFile) != propFile

Sign up to request clarification or add additional context in comments.

1 Comment

The compiler says: Cannot instantiate the type InputStream
0

Your problem is the path, try to replace for this:

String propFile  = "./connection.properties";

Comments

0

Using NIO2 you can load the file (if you have the read permissions) and convert it to InputStream like:

Path path = Paths.get("/home/devuser/connection.properties");
InputStream str = Files.newInputStream(path);

1 Comment

Flies or Files ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.