java 如何从Java设置系统变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11100967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set system variables from Java?
提问by missingfaktor
I need to set certain system variables from within the program. My Google fu is failing me in finding any way to do it. How do I do it? (I am okay with hacky approaches. I need to be able to run this app on Windows, Linux, and Mac.)
我需要从程序中设置某些系统变量。我的 Google fu 没有找到任何方法来做到这一点。我该怎么做?(我可以接受 hacky 方法。我需要能够在 Windows、Linux 和 Mac 上运行这个应用程序。)
Edit:
编辑:
Adding here my comment from below the post, as it isn't readily visible there:
在此处添加我从帖子下方发表的评论,因为在那里不容易看到:
The best link I could found was this, and it sets the variables only in memory. They do not persist after the program exit.
我能找到的最好的链接是this,它只在内存中设置变量。程序退出后它们不会持续存在。
Edit:
编辑:
I am writing an installer and need to somehow record at system level that installation happened (along with paths to some directories). The next time user runs the setup, the installer will check if the variables already exist in the system, in which case a user will be given an appropriate warning.
我正在编写一个安装程序,需要以某种方式在系统级别记录发生的安装(以及某些目录的路径)。下次用户运行安装程序时,安装程序将检查系统中是否已存在变量,在这种情况下,用户将收到适当的警告。
If twiddling with environment variables is not a good idea, what will be the best approach to achieve the above?
如果使用环境变量不是一个好主意,那么实现上述目标的最佳方法是什么?
回答by DieterDP
If you want your environment variables to persist after your program ends, I would suggest you use the Properties
class. It can be persisted to a file very easily, and vice versa.
如果您希望您的环境变量在程序结束后保持不变,我建议您使用Properties
该类。它可以很容易地持久化到一个文件中,反之亦然。
回答by vamsi
Use following methods of system class
使用系统类的以下方法
// Get a system property
String dir = System.getProperty("user.dir");
// Set a system property
String previousValue = System.setProperty("application.property", "newValue");
for more details reffer
更多详情请参考
回答by vamsi
First of all, Properties is a java class that is used to hold properties that maybe needed for your program. The basic properties that you are talking about are provided by the operating system. Not all of these can be changed. If you try, you will get a SecutrityException (You can't change the os.name for instance). The basic properties are read from the memory of the computer (basically) you can add additional variables to this by setting environment variables in the operating system you are using. Such as in Win95 you can add to the autoexec.bat the line: set BARTENDER_NAME=Carl This line can go in any batch file and BARTENDER_NAME will equal Carl until you reset it. In your java program If you add the line System.out.println(System.getProperty("BARTEDER_NAME")); You'll get Carl as the output. In the bash shell on Linux or Unix you'd use BARTENDER_NAME=Carl export BARTENDER_NAME
首先,Properties 是一个 Java 类,用于保存您的程序可能需要的属性。您所谈论的基本属性是由操作系统提供的。并非所有这些都可以更改。如果你尝试,你会得到一个 SecutrityException(例如你不能改变 os.name)。基本属性是从计算机的内存中读取的(基本上)您可以通过在您使用的操作系统中设置环境变量来添加其他变量。例如在 Win95 中,您可以在 autoexec.bat 中添加以下行: set BARTENDER_NAME=Carl 这行可以放在任何批处理文件中,并且 BARTENDER_NAME 将等于 Carl,直到您重置它为止。在你的java程序中如果你添加了 System.out.println(System.getProperty("BARTEDER_NAME")); 您将获得 Carl 作为输出。
You can create your own set of properties for your java program and store them in a file and load them using the Properties load() method. Hope this helps
您可以为 Java 程序创建自己的一组属性,并将它们存储在文件中,然后使用 Properties load() 方法加载它们。希望这可以帮助
from
从
http://www.coderanch.com/t/387634/java/java/Permanently-setting-System-property
http://www.coderanch.com/t/387634/java/java/Permanently-setting-System-property
回答by artaxerxe
Because there is not a standard solution for this, I would recommend you to use a Factory Pattern for this. It means something like:
因为没有标准的解决方案,我建议您为此使用工厂模式。它的意思是这样的:
envManager = null
envManager = null
if
system is Windows
if
系统是 Windows
`envManager = WindowsEnvManager`
else if
system is Linux
else if
系统是 Linux
`envManager = LinuxEnvManager`
else if
system is Mac
else if
系统是 Mac
`envManager = macEnvManager`
persistEnvironment(envManager);
persistEnvironment(envManager);
and the persistEnvironment
method would call the specific functions on EnvManager
.
并且该persistEnvironment
方法将调用 上的特定函数EnvManager
。
回答by maba
How about using Java Preferences API. That way you would store this kind of data in the Registry if you run on Windows. Simple tutorial here.
如何使用Java Preferences API。这样,如果您在 Windows 上运行,您可以将此类数据存储在注册表中。简单教程在这里。
You can store the preferences per system or per user and the preferences are persistent as well as you desire.
您可以存储每个系统或每个用户的首选项,并且这些首选项可以根据您的需要保持不变。
Edit
编辑
Example:
例子:
package com.stackoverflow.Q11100967;
import java.util.prefs.Preferences;
/**
* @author maba, 2012-06-20
*/
public class App {
public static void main(String[] args) {
Preferences preferences = Preferences.systemNodeForPackage(App.class);
if (!preferences.getBoolean("installed", false)) {
// Install the stuff...
preferences.putBoolean("installed", true);
preferences.put("version", "1.2.3");
}
}
}
On Windows the preferences will be stored at HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Prefs/com/stackoverflow/Q11100967
.
在 Windows 上,首选项将存储在HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Prefs/com/stackoverflow/Q11100967
.
In order for this to work you have to run your process with admin privileges or a similar approach.
为了使其工作,您必须使用管理员权限或类似方法运行您的进程。
Edit2
编辑2
On Linux the preferences would be stored at /etc/.java/.systemPrefs/com/stackoverflow/Q11100967/
in a file called prefs.xml
with the following content:
在 Linux 上,首选项将存储在具有以下内容/etc/.java/.systemPrefs/com/stackoverflow/Q11100967/
的文件中prefs.xml
:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE map SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<map MAP_XML_VERSION="1.0">
<entry key="installed" value="true"/>
<entry key="version" value="1.2.3"/>
</map>
回答by Thihara
Ok this is off the top of my head so it's extremely hacky and stuff.
好吧,这是我的头顶,所以它非常hacky 和东西。
Get hold of a process and run the command line command that will set the system variables. This isn't portable but it should suffice for short term till you find a better solution.
获取进程并运行将设置系统变量的命令行命令。这不是便携式的,但在您找到更好的解决方案之前,短期内应该足够了。