一个比较恶心的方法是调用Runtime.exec
直接使用win或者unix下查看环境变量的命令
利用process的输入/出流读取
直接使用win或者unix下查看环境变量的命令
利用process的输入/出流读取
解决方案 »
- 一个截取字段传参的问题
- 我想请问下一个类型转换的问题
- 高分送人。
- 传说中的think in java怎么有好多呀.到底是哪一本最好
- 如何实现3层依次Overlay的JPanel(大小相同),而且每层JPanel均为可见,即上2层的JPanel为“透明”
- 怎么在java application下用ie打开一个网页?100分
- 如果获取数据库中的表名!
- 在jtable里我用getValueat(...)返回是object,怎样把它转换为string?
- 请问什么地方有JAVA 的联机文档下啊????
- netty怎么才能一次接收超过1024字节的字符串
- Java 日期选择控件 DateChooser (推荐)
- java 网络编程 面对重定向怎么保留session??
System.getenv();具体请查看帮助文档。
getProperty
public static String getProperty(String key,
String def)Gets the system property indicated by the specified key.
First, if there is a security manager, its checkPropertyAccess method is called with the key as its argument. If there is no current set of system properties, a set of system properties is first created and initialized in the same manner as for the getProperties method.
Parameters:
key - the name of the system property.
def - a default value.
Returns:
the string value of the system property, or the default value if there is no property with that key.
Throws:
SecurityException - if a security manager exists and its checkPropertyAccess method doesn't allow access to the specified system property.
NullPointerException - if key is null.
IllegalArgumentException - if key is empty.
See Also:
setProperty(java.lang.String, java.lang.String), SecurityManager.checkPropertyAccess(java.lang.String), getProperties()
getenv
public static String getenv(String name)Deprecated. The preferred way to extract system-dependent information is the system properties of the java.lang.System.getProperty methods and the corresponding getTypeName methods of the Boolean, Integer, and Long primitive types. For example:
String classPath = System.getProperty("java.class.path",".");
if (Boolean.getBoolean("myapp.exper.mode"))
enableExpertCommands();
Gets an environment variable. An environment variable is a system-dependent external variable that has a string value. Parameters:
name - of the environment variable
Returns:
the value of the variable, or null if the variable is not defined.
See Also:
Boolean.getBoolean(java.lang.String), Integer.getInteger(java.lang.String), Integer.getInteger(java.lang.String, int), Integer.getInteger(java.lang.String, java.lang.Integer), Long.getLong(java.lang.String), Long.getLong(java.lang.String, long), Long.getLong(java.lang.String, java.lang.Long), getProperties(), getProperty(java.lang.String), getProperty(java.lang.String, java.lang.String)
在运行时设置一个环境变量 debug 为 true: java -Ddebug=true YourClass在程序中设置一个环境变量 debug 为 true: System.setProperty( "debug", "true" );获取一个环境变量 debug : String debug = System.getProperty( "debug" );下表中列出了一部分环境变量,这些是 Java 已经定义好的,可以在程序中通过 System.getProperty( "key" ) 来获取,更多的环境变量请参阅 JDK 文档。
属性名 描 述
java.version Java 运行时版本
java.home Java 的安装目录
java.class.version Java 类格式的版本号
java.class.path Java 类的查找路径
java.io.tmpdir 默认的临时目录
java.compiler Java 所使用的及时编译器
java.ext.dirs Java 扩展包的目录
os.name 操作系统的名称
os.arch 操作系统的体系结构
os.version 操作系统的版本
file.separator 文件分隔符(Unix 下为'/')
path.separator 路径分隔符(Unix 下为':')
line.separator 换行符(Unix 下为'\n')
user.name 用户帐号名
user.home 用户目录
user.dir 用户当前的工作目录
那么,在JAVA程序中用什么方法来得到env1的变量值。
(既 env1 是透明的,而env1的值是不知道的)
java不是号称最安全的?
所以,不能.因为他没提供这样的接口.
除非你自己写dll来调用.
但按您的需求,在手机里运行,就不可能了.
Process ObjPrcess = ObjRunTime.exec("cmd /c echo %env1%");
那么,在UNIX怎么直接读取呢?
因为他没提供这样的接口.
除非你自己写dll来调用.
但按您的需求,在手机里运行,就不可能了”还不如你自己说的方法来的简单呢:“在WINDOWS下可以用以下语句直接读取
Process ObjPrcess = ObjRunTime.exec("cmd /c echo %env1%");
那么,在UNIX怎么直接读取呢?”