我正在实现一小包程序供批处理用户使用。 这个包中的几乎所有东西都是用 C++ 制作的,并从 Java 调用。
如何为要使用的批处理文件设置环境变量?
我试过用这个:
JNIEXPORT void JNICALL METHOD_NAME(JNIEnv *env, jclass theclass, jstring key, jstring value) {
const char* thekey = env->GetStringUTFChars(key, false);
const char* thevalue = env->GetStringUTFChars(value, false);
std::string envvar;
envvar.append(thekey);
envvar.append("=");
envvar.append(thevalue);
_putenv(envvar.c_str());
env->ReleaseStringUTFChars(key, thekey);
env->ReleaseStringUTFChars(value, thevalue);
}
但是批处理文件没有看到任何新变量。
我应该使用 system("set thing=value");
吗?
最佳答案
经过一些研究,我得出的结论是子进程不能修改父进程的环境。
关于c++ - 从 C++ 为 Batch 设置临时环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39435810/