使用脚本运行程序读取.properties文件

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命令转换编码格式;









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值