was环境变量java修改,如何使用exec在Java中设置环境变量?

在Java中尝试通过Runtime.exec()方法设置并读取环境变量时,遇到问题。由于新启动的进程会复制当前环境变量,对环境变量的修改只在其内部生效,不会影响到调用者。因此,尝试直接读取时,FOO变量始终为null。解决此类问题需要理解进程间的环境变量隔离。实际目标应是找到正确设置和读取Java环境变量的方法。
摘要由CSDN通过智能技术生成

I'm trying to set an environment variable, and read it back to verify it was actually set.

I've got the following :

import java.io.IOException;

public class EnvironmentVariable

{

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

{

Runtime.getRuntime().exec("cmd.exe set FOO=false");

String s = System.getenv("FOO");

System.out.println(s);

}

}

However, it appears that FOO is always null, meaning its probably not set correctly.

Do I have the exec command correct? The javadocs state it can take a string argument as the command.

Any ideas?

解决方案

This won't work. When you start a new process, that process receives a copy of the environment. Any changes it then makes to environment variables are made within that copy, and at no point will become visible to the caller.

What are you actually trying to achieve?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值