jar 中没有配置文件的 Java jar 创建

标签 java eclipse jar

我正在从 eclipse 中创建可运行的 jar 文件,我的要求是 jar 没有任何属性文件,如 log4j.propertiesconfig.properties 但是当我创建时来自波纹管代码的 jar 我的 jar 包含两个属性文件,
那么,如果没有这个属性文件,我该如何创建 jar 文件呢?

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());
        }
    }
}

最佳答案

您必须将您的文件名作为参数传递给您的 jar 文件,它只会将所需的类放入 jar 文件,而不是您的配置文件。

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());
        }
    }
}

关于jar 中没有配置文件的 Java jar 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27498550/

相关文章:

java - 将特定的外部库包含到 Gradle 构建的 jar 文件中

java - 如何将矩阵变成偶数维?

android - 将屏幕尺寸(像素)转换为纬度和经度

c - 在 Eclipse 索引器中处理 Keil C51 关键字

Java jar - 没有主要 list 属性

java - 如何从阴影 Maven jar 中排除测试依赖项?

java - 在JAVA中将base64图像转换为Sql Blob

Java - 更改数据字段?

java - 如何在Android上实现OnClickListener

eclipse - eclipse 中的 tomcat - 由于先前的错误启动失败