I have two packages in a Java project.
Package A has a class called App.java. This class accepts argument parsing (leveraging apache commons-cli lib).
The code is
CommandLineParser clip = new DefaultParser();
Options options = new Options();
options.addOption("zp", "ZipFilePath", true, "Mention the path where zip file is present");
options.addOption("d", "dbPropFile", true, "Mention the path database property file");
options.addOption("h", "help", false, "This mentions how to use this utility");
CommandLine cli = clip.parse(options, args);
File dbpropFile = new File(cli.getOptionValue("d"));
Now how can I import dbPropFile variable in another class (SQLServerConn.java) of package B?
package com.abc.integra.db.B;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import com.abc.integra.db.A.LoadDBProps;
public class SQLServerConn {
public static Connection dbConn;
public static Properties props;
public static Connection getConn() {
try {
final String drivername = (String)SQLServerConn.props.get("drivername");
final String url = (String)SQLServerConn.props.get("url");
final String dbname = (String)SQLServerConn.props.get("dbname");
final String username = (String)SQLServerConn.props.get("username");
final String password = (String)SQLServerConn.props.get("password");
//jasypt final String password1 = (String)SQLServerConn.props.getProperty(key);
Class.forName(drivername); //registering the driver before connection with the DB
SQLServerConn.dbConn = DriverManager.getConnection(url + ";databaseName=" + dbname + ";user=" + username + ";password=" + password);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
return SQLServerConn.dbConn;
}
static {
SQLServerConn.dbConn = null;
try {
final LoadDBProps lp = new LoadDBProps();
SQLServerConn.props = lp.loadDBProperties(filename);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I want to substitute the dbpropFile variable value in the place of filename in load properties section.
You have to give full path to import the class. For Example:
Now you have access to the App class, you can use either:
1) The App object to access it's public class member (composition),
2) Direct class name to access the static and public class members.
If you are asking to only import single property
dbpropFile, it's not possible in java.