wmic java_Java执行wmic命令获取系统环境变量

1.首先编写文件setenv.bat设置系统环境变量:

echo %cd%

set framework_home=%cd%

echo %framework_home%

wmic ENVIRONMENT create name="framework_home",username="",VariableValue="%framework_home%"

Note:

此处假设framework_home为要添加的系统环境变量,且其值为当前路径。Windows系统有两种环境变量:用户变量和系统变量。上面设置的是系统变量。

2.编写Java程序读取上面设置的系统环境变量:

package test.util;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

class StreamDrainerThread implements Runnable

{

private InputStream ins;

public static String environmentValue;

public StreamDrainerThread(InputStream ins)

{

this.ins = ins;

}

public void run()

{

try

{

BufferedReader reader = new BufferedReader(new InputStreamReader(ins));

String line = null;

while ((line = reader.readLine()) != null)

{

if (!line.trim().equals(""))

{

environmentValue = line;

}

}

} catch (Exception e)

{

e.printStackTrace();

}

}

}

public class WMICJava

{

public static void main(String[] args) throws IOException

{

String[] cmd = new String[]

{ "cmd.exe", "/C", "wmic ENVIRONMENT where \"name=\'framework_home\'\" get VariableValue" };

try

{

Process process = Runtime.getRuntime().exec(cmd);

StreamDrainerThread streamDrainer = new StreamDrainerThread(process.getInputStream());

new Thread(streamDrainer).start();

process.getOutputStream().close();

process.waitFor();

System.out.println("Environment Parameter OATS_HOME is " + StreamDrainerThread.environmentValue);

} catch (Exception e)

{

e.printStackTrace();

}

}

}

参考:

【1】java Runtime 执行 windows wmic 命令

http://hi.baidu.com/kelongxhu/blog/item/7ed96dfb028c9b354f4aeab1.html

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-05-30 21:14

浏览 306

评论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值