我正在从 eclipse 中创建可运行的 jar 文件,我的要求是 jar 没有任何属性文件,如 log4j.properties
和 config.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/