Import java.io.File variable in another class

221 views Asked by At

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.

1

There are 1 answers

1
Zain Ul Abideen On

You have to give full path to import the class. For Example:

import xx.A.App;

Now you have access to the App class, you can use either:

1) The App object to access it's public class member (composition),

class App {
  File dbPropFile;
  CommandLineParser clip = new DefaultParser();
  public void setDbPropFile (String fileOption) {
       this.dbPropFile = new File(clip.getOptionValue(fileOption));
  }

  public File getDbPropFile () {
       return dbPropFile;
  }
}

public class SQLServerConn {
  App app = new App();
  app.setDbPropFile("d");
  SQLServerConn.props = lp.loadDBProperties(app.getDbPropFile());
}

2) Direct class name to access the static and public class members.

class App {
  static File dbpropFile = new File(cli.getOptionValue("d"));
}

class SQLServerConn  {
   SQLServerConn.props = lp.loadDBProperties(App.dbpropFile); // exact class name to access static variable
}

If you are asking to only import single property dbpropFile, it's not possible in java.