使用JAVA更新独立于OS的PATH环境变量[重复]

时间:2022-06-03 11:59:00

This question already has an answer here:

这个问题在这里已有答案:

How can I update the environment variable PATH using java. With the same code I should be able to update the PATH variable in Windows and Unix when I run the code in respective OS.

如何使用java更新环境变量PATH。使用相同的代码,当我在相应的操作系统中运行代码时,我应该能够在Windows和Unix中更新PATH变量。

Thanks in advance.

提前致谢。

regards, San

1 个解决方案

#1


0  

In Unix (c language) you can change the PATH by using the different forms of "exec" command, but it can only be used to pass additional/updated variables to whatever will be executed by the current program.

在Unix(c语言)中,您可以使用不同形式的“exec”命令来更改PATH,但它只能用于将其他/更新的变量传递给当前程序将执行的任何操作。

One of the principles of Unix is that the environment is unchanged from what it was when you started to execute your code. So, even if your code (java) is executed thru a JVM, it will still have to comply to this rule. Once the JVM exits, all updates to PATH are lost.

Unix的一个原则是环境与开始执行代码时的环境没有变化。因此,即使您的代码(java)通过JVM执行,它仍然必须遵守此规则。一旦JVM退出,对PATH的所有更新都将丢失。

This is not the case in Windows. A program can change the current environment, which is why you often find yourself in a different directory after you executed some code on a windows platform.

在Windows中不是这种情况。程序可以更改当前环境,这就是为什么在Windows平台上执行某些代码后,您经常会发现自己位于不同的目录中。

Unfortunately, because of this, you will not have a means of doing that in both Windows and Unix. The best you can hope for in Unix is to have something like this:

不幸的是,正因为如此,你无法在Windows和Unix中做到这一点。在Unix中你能想到的最好的就是拥有这样的东西:

myScript.sh:

# This is a script that executes the program and then 'includes' a script
/path/to/my/program  # which writes the following file containing environment changes
. /this/file/created/by/above

in 'shell' interpreter (command prompt if you prefer):

在'shell'解释器中(如果您愿意,可以使用命令提示符):

# Execute my program using myScript.sh
. ./myScript.sh

This should preserve your path change but you can never call 'myScript.sh' without the leading dot. If you forgot it, you could do an include of the "/this/file/created/by/above" after the execution.

这应该保留你的路径改变,但你不能在没有前导点的情况下调用'myScript.sh'。如果你忘了它,你可以在执行后包含“/ this / file / created / by / above”。

Of course when you use this you need to handle possible simultaneous execution by multiple users. So the file "/this/file/created/by/above" should be distinct for each execution. Investigate the possible use of "$$" parameter.

当然,当您使用它时,您需要处理多个用户可能同时执行的操作。因此,对于每次执行,文件“/ this / file / created / by / above”应该是不同的。调查可能使用“$$”参数。

Ps. "/path/to/my/program" can be a JVM that will execute your java code.

PS。 “/ path / to / my / program”可以是一个执行java代码的JVM。

Security: If I was a "root" user I would definitively not want to execute your code. Your program could put anything in that file "/this/file/created/by/above" and it would get executed by root.

安全性:如果我是“root”用户,我肯定不想执行你的代码。你的程序可以在该文件中放入任何内容“/ this / file / created / by / above”,它将由root执行。

#1


0  

In Unix (c language) you can change the PATH by using the different forms of "exec" command, but it can only be used to pass additional/updated variables to whatever will be executed by the current program.

在Unix(c语言)中,您可以使用不同形式的“exec”命令来更改PATH,但它只能用于将其他/更新的变量传递给当前程序将执行的任何操作。

One of the principles of Unix is that the environment is unchanged from what it was when you started to execute your code. So, even if your code (java) is executed thru a JVM, it will still have to comply to this rule. Once the JVM exits, all updates to PATH are lost.

Unix的一个原则是环境与开始执行代码时的环境没有变化。因此,即使您的代码(java)通过JVM执行,它仍然必须遵守此规则。一旦JVM退出,对PATH的所有更新都将丢失。

This is not the case in Windows. A program can change the current environment, which is why you often find yourself in a different directory after you executed some code on a windows platform.

在Windows中不是这种情况。程序可以更改当前环境,这就是为什么在Windows平台上执行某些代码后,您经常会发现自己位于不同的目录中。

Unfortunately, because of this, you will not have a means of doing that in both Windows and Unix. The best you can hope for in Unix is to have something like this:

不幸的是,正因为如此,你无法在Windows和Unix中做到这一点。在Unix中你能想到的最好的就是拥有这样的东西:

myScript.sh:

# This is a script that executes the program and then 'includes' a script
/path/to/my/program  # which writes the following file containing environment changes
. /this/file/created/by/above

in 'shell' interpreter (command prompt if you prefer):

在'shell'解释器中(如果您愿意,可以使用命令提示符):

# Execute my program using myScript.sh
. ./myScript.sh

This should preserve your path change but you can never call 'myScript.sh' without the leading dot. If you forgot it, you could do an include of the "/this/file/created/by/above" after the execution.

这应该保留你的路径改变,但你不能在没有前导点的情况下调用'myScript.sh'。如果你忘了它,你可以在执行后包含“/ this / file / created / by / above”。

Of course when you use this you need to handle possible simultaneous execution by multiple users. So the file "/this/file/created/by/above" should be distinct for each execution. Investigate the possible use of "$$" parameter.

当然,当您使用它时,您需要处理多个用户可能同时执行的操作。因此,对于每次执行,文件“/ this / file / created / by / above”应该是不同的。调查可能使用“$$”参数。

Ps. "/path/to/my/program" can be a JVM that will execute your java code.

PS。 “/ path / to / my / program”可以是一个执行java代码的JVM。

Security: If I was a "root" user I would definitively not want to execute your code. Your program could put anything in that file "/this/file/created/by/above" and it would get executed by root.

安全性:如果我是“root”用户,我肯定不想执行你的代码。你的程序可以在该文件中放入任何内容“/ this / file / created / by / above”,它将由root执行。