如何使用Java代码和JNA在Windows上编辑系统或用户变量?我只发现this问题,但似乎没有帮助。为什么以下两个代码片段不起作用?
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Environment
{
public interface WinLibC extends Library
{
WinLibC INSTANCE = (WinLibC) Native.loadLibrary("msvcrt",
WinLibC.class);
public int _putenv(String name);
}
public static void main(String[] args)
{
WinLibC.INSTANCE._putenv("MYVARIABLE=MYVALUE");
}
}
和
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Environment
{
WinLibC clib = (WinLibC) Native.loadLibrary("msvcrt", WinLibC.class);
public Environment()
{
setenv("myVariable", "myValue");
}
public interface WinLibC extends Library
{
public int _putenv(String name);
}
public void setenv(String variable, String value)
{
clib._putenv(variable + "=" + value);
}
public static void main(String[] args)
{
new Environment();
}
}
没有编写环境变量,没有任何反应。