1).properties文件
name=xiaoxi
age=24
2)读取代码
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesTest {
private Properties defaultProperties = new Properties();
public PropertiesTest(){
}
private void getProperties(String properties){
try {
InputStream in = new FileInputStream(properties);
this.defaultProperties.load(in);
in.close();
String name = this.defaultProperties.getProperty("name");
System.out.println("name: " + name);
int age = Integer.valueOf(this.defaultProperties.getProperty("age"));
System.out.println("age: " + age);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
if ((args == null) || (args.length == 0)) {
System.out.println("java -jar propertiesTest.jar [属性文件]");
return;
}
String TEST_HOME = null;
try {
TEST_HOME = System.getenv("TEST_HOME");
} catch (Exception e) {
}
String testPro = args[0];
System.out.println("testPro: " + testPro);
String properties = TEST_HOME + File.separator + "conf" + File.separator + "pro.properties"
PropertiesTest1 pt = new PropertiesTest1();
pt.getProperties(properties);
}
}
3)运行脚本文件start.sh
#!/bin/bash
#TEST_HOME="/home/logmonitor/LogStat"
export TEST_HOME=/home/xiaoxi/test
TESTPRO="hello"
echo "java -jar $TEST_HOME/core/providerLogMonth.jar $TESTPRO"
nohup java -jar $TEST_HOME/core/providerLogMonth.jar $TESTPRO > $TEST_HOME/log/test.log 2>&1 &
4)输出结果
testPro: hello
name: xiaoxi
age: 24
注:
1)将程序打成 jar包和pro.properties文件添加到Linux服务器上;
2)使用脚本start.sh运行程序,sh start.sh;
3)TESTPRO为运行程序时添加的参数;
4)程序将TEST_HOME作为环境变量读取,TEST_HOME = System.getenv("TEST_HOME");
5) File.separetor为文件分隔符;
6)脚本中echo为运行程序时的回显语句;
7)如果脚本是在Windows环境先编写的放到Linux环境时需执行dos2unix start.sh命令转换编码格式;