使用Java设置Windows系统变量 [英] Set Windows System Variables with Java

查看:211
本文介绍了使用Java设置Windows系统变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将特定目录添加到Windows系统变量%PATH%?
这似乎不起作用:

is there a way to add a specific directory to the Windows systemvariable %PATH%? This doesn't seem to work:

String[] cmd = { "cmd", "/c", "set", "PATH=\"%PATH%;c:\\test\"" };
Runtime.getRuntime().exec( cmd );

c:\ test \未出现在System.getenv(PATH)中;或者输出

c:\test\ doesn't appear in System.getenv("PATH"); or in the output of

String[] cmd = { "cmd", "/c", "echo", "%PATH%" };
Runtime.getRuntime().exec( cmd );

我需要修改Windows下当前Java进程的%PATH%变量。原因是,我需要加载一些相互交叉引用的本机dll文件。所以我想将应用程序路径添加到Windows环境中。

What I need is to modify the %PATH%-variable for the current Java-Process under Windows. The reason is, that I need to load some native dll-files which cross-reference each other. So I'd like to add the application-path to the Windows environment.

我接下来尝试的是用于C函数的小型JNI-Wrapperputenv看起来像这样:

The next thing I tried was a small JNI-Wrapper for the C-Function "putenv" which looks like this:

JNIEXPORT void JNICALL Java_com_splitscreen_AppletTest_PutEnv_putEnv
  (JNIEnv *env, jobject jobj, jstring val) {

    jboolean iscopy;

    const char *mvalue = (*env)->GetStringUTFChars(
                env, val, &iscopy);

    putenv(mvalue);
}

这就是我所说的:

final String curPath = System.getenv( "PATH" );
final PutEnv pe = new PutEnv();
pe.putEnv( "PATH=" + curPath + ";c:\test" );

final String newPath = System.getenv( "PATH" );
System.out.println( newPath );

但是路径是平等的。我不确定Java-System-Environment的Map是否未更新或者putenv是否不起作用。有没有办法检查这个?

But the pathes are equal. I'm not sure whether the Map of the Java-System-Environment isn't updated or whether putenv didn't work. Is there a way to check this?

推荐答案

这个不起作用的原因是两个 exec()调用启动两个不同的shell;您设置路径的那个不是您签入的路径。

The reason this doesn't work is that the two exec() invocations start two different shells; the one you set the path in isn't the one you check it in.

很难更改永久的系统范围路径设置。但是你可以在调用你需要的一个或多个程序的过程中改变路径。

It's difficult to change the permanent, systemwide path setting. But you can change the path for the duration of the invocation of one or more programs that you need it for.

具体来说,要做的就是自己写一个批处理文件( .CMD .BAT ,如您所愿),设置 PATH 接近开头,按照你想用该路径执行的任何DOS / Windows命令,然后 exec()该脚本文件。

Specifically, the thing to do is to write yourself a batch file (.CMD or .BAT, as you please), set the PATH near the beginning, follow that with whatever DOS/Windows commands you'd like executed with that path, and then exec() that script file.

更新当前 Java 进程的PATH似乎毫无意义。 Java一旦运行,就不关心路径了。或者你正在运行一些库代码吗?

Updating the PATH for the current Java process seems pretty pointless. Java, once running, doesn't care about the path. Or are you running some library code that does?

如果你使用 exec()运行来自Java的DOS / Windows命令,上面的技巧将起作用。

If you are running DOS/Windows commands from Java using exec(), the above trick will work.

更新:好的,你有图书馆代码因为它自己想要PATH这样设置,你想给它它想要的东西。

Update: OK, you have library code that for reasons of its own wants the PATH set just so, and you want to give it what it wants.

我在这里考虑的是启动一个新的JVM 。您可以使用 exec(cmd,envp)在<$ c $中使用一组自定义环境变量启动一个新的Java应用程序(你自己,在一个紧要关头) C> envp 。只需复制已存在的那些并操纵 PATH 的内容(如果有的话)。

What I would consider here is to fire up a new JVM. You can use exec(cmd, envp) to start up a new Java application ("yourself," in a pinch) with a custom set of environment variables in envp. Just copy the ones that are already there and manipulate the contents of PATH, if any.

标准方法启动一个新的Java应用程序是创建一个新的 ClassLoader ,并且有关于如何实现它的各种描述。但我不确定你是否可以使用该程序来创建一个新环境 - 所以 exec -JVM可能不仅更简单,而且可能是唯一的方法。

The standard way to start up a new Java app is to create a new ClassLoader, and there are various descriptions on how to accomplish that. But I'm not sure you can use that procedure to come up with a new environment - so exec-ing the JVM may not only be simpler, but possibly the only way.

这篇关于使用Java设置Windows系统变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆