简体   繁体   中英

Java jar creation without configuration files in jar

I am crating runnable jar file from eclipse, my requirement is jar don't have any properties file like log4j.properties and config.properties but when I am creating jar from bellow code my jar contains both properties file,
So, how can I create jar file without this properties file?

public class HadoopFileCreation1 {
    public final static Logger logger = Logger.getLogger(HadoopFileCreation1.class);

    public String fileName = null;

    static Properties prop = new Properties();

    public static void main(String[] args) {
        try {
            System.out.println("---------START------------");
            PropertyConfigurator.configure("properties/log4j.properties");
            HadoopFileCreation1 hfc = new HadoopFileCreation1();
            hfc.readProperty();

            hfc.writeDATFileForHadoop("PORT", getPropValues("START_TIME"));
            hfc.writeDATFileForHadoop("VLAN", getPropValues("START_TIME"));

            System.out.println("---------END------------");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }

    }

    public void readProperty() {
        try {
            prop = new Properties();
            String propFileName = "properties/config.properties";
            File file = new File(propFileName);
            FileInputStream inputStream = new FileInputStream(file);
            prop.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }
}

I have done that in Eclipse simply by storing those configurations files elsewhere in the file system, changing the paths pointing to them in your program and then exporting to jar. In my case I had another properties file in the classes folder to the the jar where in the file system it can find the actual configuration.

  public void loadConf(){
    try {
        // loading the properties
        prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("rutaproperties.properties"));
        String propertiesPath = prop.getProperty("properties.path");
        FileInputStream in = new FileInputStream(propertiesPath );
        prop.load(in);
        in.close();

        FileInputStream in_audit = new FileInputStream(prop.getProperty("audit.props.path"));
        prop.load(in_audit);
        in_audit.close();
        try{
            // log config
            String logPath = prop.getProperty("log4j.path.conf");
            if(new File(logPath ).exists()){
                PropertyConfigurator.configure(logPath );
            }else 
                throw new FileNotFoundException("Path" + ruta_log + " not found.");
            log.info("configuration loaded.");
            log.info("App ready.");
        }catch (Exception e) {
            String msgError = "Error configuring the log. "
                    + e.getMessage();
            log.error(msgError);
            e.printStackTrace();
        }
    }catch (Exception e) {
        String msgError = "Fatal Error. Check properties files."
                + e.getMessage();
        log.error(msgError);
        throw new RuntimeException(msgError);
    }

  }

You should use the exclude feature from the source tab in your project's Java build path .

Basically, you can set a pattern to exclude one or more files from your source path, hence they will not be included in your jar.

More information here : https://stackoverflow.com/a/1489225/2003986

You have to pass your file's name as argument to your jar file, it will take only the required classes into the jar file, not your configuration files.

public class HadoopFileCreation1 {
    public final static Logger logger = Logger.getLogger(HadoopFileCreation1.class);

    public String fileName = null;
    private String log4jFile=null,configFile=null;
    static Properties prop = new Properties();

    public static void main(String[] args) {
        try {

             log4jFile = args[0];
             configFile = args[1];

            System.out.println("---------START------------");
            PropertyConfigurator.configure(log4jFile );
            HadoopFileCreation1 hfc = new HadoopFileCreation1();
            hfc.readProperty(configFile);

            hfc.writeDATFileForHadoop("PORT", getPropValues("START_TIME"));
            hfc.writeDATFileForHadoop("VLAN", getPropValues("START_TIME"));

            System.out.println("---------END------------");

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }

    }

    public void readProperty(String configFile) {
        try {
            prop = new Properties();
            String propFileName = configFile;
            File file = new File(propFileName);
            FileInputStream inputStream = new FileInputStream(file);
            prop.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM