我正在从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());
}
}
}
答案 0 :(得分:0)
我在Eclipse中完成了这一操作,只需将这些配置文件存储在文件系统的其他位置,在程序中更改指向它们的路径,然后导出到jar。在我的情况下,我在classes文件夹中有另一个属性文件到jar,在文件系统中它可以找到实际的配置。
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);
}
}
答案 1 :(得分:0)
您应该使用项目的 Java构建路径中的源选项卡中的排除功能。
基本上,您可以设置模式以从源路径中排除一个或多个文件,因此它们不会包含在您的jar中。
答案 2 :(得分:0)
您必须将文件的名称作为参数传递给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());
}
}
}