Reputation: 2083
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 ?
Upvotes: 0
Views: 132
Reputation: 31473
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);
Upvotes: 0
Reputation: 712
InputStream inputStream = new FileInputStream(propFile);
Thats it...
Because
StartCount.class.getClassLoader().getResourceAsStream(propFile) != propFile
Upvotes: 1
Reputation: 1403
Your problem is the path, try to replace for this:
String propFile = "./connection.properties";
Upvotes: 0