- 浏览: 321900 次
- 来自: ...
文章分类
最新评论
-
梦回秘蓝:
whichisnotuse 写道非也, 除非需要很执行一段代码 ...
什么情况需要 if (log.isDebugEnabled()) {} -
wupy:
一直while的话会不会太耗内存呢?
java socket 长连接 短连接 -
xy2401:
<type-mapping> <sql-t ...
hibernate tools oracle nvarchar2 被映射成Serializable -
xy2401:
xy2401 写道额,原来配置文件可以改啊。。。不过我已经生成 ...
hibernate tools oracle nvarchar2 被映射成Serializable -
郭玉成:
你好, 我已经找到原因了!
java 编译非文本形式的文件
http://www.rgagnon.com/javadetails/java-0150.html
JDK up to 1.4
Start the JVM with the "-D" switch to pass properties to the application and read them with the System.getProperty() method.
SET myvar=Hello world SET myothervar=nothing java -Dmyvar="%myvar%" -Dmyothervar="%myothervar%" myClass
then in myClass
String myvar = System.getProperty("myvar"); String myothervar = System.getProperty("myothervar");
If you don't know in advance, the name of the variable to be passed to the JVM, then there is no 100% Java way to retrieve them.
One approach (not the easiest one), is to use a JNI call to fetch the variables, see this HowTo.
A more low-tech way, is to launch the appropriate call to the operating system and capture the output. The following snippet puts all environment variables in a Properties class and display the value the TEMP variable.
import java.io.*; import java.util.*; public class ReadEnv { public static Properties getEnvVars() throws Throwable { Process p = null; Properties envVars = new Properties(); Runtime r = Runtime.getRuntime(); String OS = System.getProperty("os.name").toLowerCase(); // System.out.println(OS); if (OS.indexOf("windows 9") > -1) { p = r.exec( "command.com /c set" ); } else if ( (OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1 ) || (OS.indexOf("windows xp") > -1) ) { // thanks to JuanFran for the xp fix! p = r.exec( "cmd.exe /c set" ); } else { // our last hope, we assume Unix (thanks to H. Ware for the fix) p = r.exec( "env" ); } BufferedReader br = new BufferedReader ( new InputStreamReader( p.getInputStream() ) ); String line; while( (line = br.readLine()) != null ) { int idx = line.indexOf( '=' ); String key = line.substring( 0, idx ); String value = line.substring( idx+1 ); envVars.setProperty( key, value ); // System.out.println( key + " = " + value ); } return envVars; } public static void main(String args[]) { try { Properties p = ReadEnv.getEnvVars(); System.out.println("the current value of TEMP is : " + p.getProperty("TEMP")); } catch (Throwable e) { e.printStackTrace(); } } }
thanks to w.rijnders for the w2k fix.
An update from Van Ly :
I found that, on Windows 2003 server, the property value for "os.name" is actually "windows 2003." So either that has to be added to the bunch of tests or just relax the comparison strings a bit:
else if ( (OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1 ) || (OS.indexOf("windows 2003") > -1 ) // ok // but specific to 2003 || (OS.indexOf("windows xp") > -1) ) {
else if ( (OS.indexOf("nt") > -1) || (OS.indexOf("windows 20") > -1 ) // better, // since no other OS would // return "windows" || (OS.indexOf("windows xp") > -1) ) {
I started with "windows 200" but thought "what the hell" and made it "windows 20" to lengthen its longivity. You could push it further and use "windows 2," I suppose. The only thing to watch out for is to not overlap with "windows 9."
On Windows, pre-JDK 1.2 JVM has trouble reading the Output stream directly from the SET command, it never returns. Here 2 ways to bypass this behaviour.
First, instead of calling directly the SET command, we use a BAT file, after the SET command we print a known string. Then, in Java, when we read this known string, we exit from loop.
[env.bat] @set @echo **end [java] ... if (OS.indexOf("windows") > -1) { p = r.exec( "env.bat" ); } ... while( (line = br.readLine()) != null ) { if (line.indexOf("**end")>-1) break; int idx = line.indexOf( '=' ); String key = line.substring( 0, idx ); String value = line.substring( idx+1 ); hash.put( key, value ); System.out.println( key + " = " + value ); }
The other solution is to send the result of the SET command to file and then read the file from Java.
... if (OS.indexOf("windows 9") > -1) { p = r.exec( "command.com /c set > envvar.txt" ); } else if ( (OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1 || (OS.indexOf("windows xp") > -1) ) { // thanks to JuanFran for the xp fix! p = r.exec( "cmd.exe /c set > envvar.txt" ); } ... // then read back the file Properties p = new Properties(); p.load(new FileInputStream("envvar.txt"));
Thanks to JP Daviau
// UNIX public Properties getEnvironment() throws java.io.IOException { Properties env = new Properties(); env.load(Runtime.getRuntime().exec("env").getInputStream()); return env; } Properties env = getEnvironment(); String myEnvVar = env.get("MYENV_VAR");
To read only one variable :
// NT version , adaptation for other OS is left as an exercise... Process p = Runtime.getRuntime().exec("cmd.exe /c echo %MYVAR%"); BufferedReader br = new BufferedReader ( new InputStreamReader( p.getInputStream() ) ); String myvar = br.readLine(); System.out.println(myvar);
Java's System properties contains some useful informations about the environment, for example, the TEMP and PATH environment variables (on Windows).
public class ShowSome { public static void main(String args[]){ System.out.println("TEMP : " + System.getProperty("java.io.tmpdir")); System.out.println("PATH : " + System.getProperty("java.library.path")); System.out.println("CLASSPATH : " + System.getProperty("java.class.path")); System.out.println("SYSTEM DIR : " + System.getProperty("user.home")); // ex. c:\windows on Win9x System.out.println("CURRENT DIR: " + System.getProperty("user.dir")); } }
Here some tips from H. Ware about the PATH on different OS.
PATH is not quite the same as library path. In unixes, they are completely different---the libraries typically have their own directories.
System.out.println("the current value of PATH is: {" + p.getProperty("PATH")+"}"); System.out.println("LIBPATH: {" + System.getProperty("java.library.path")+"}");
gives
the current value of PATH is: {/home/hware/bin:/usr/local/bin:/usr/xpg4/bin:/opt/SUNWspro/bin: /usr/ucb:/bin:/usr/bin:/home/hware/linux-bin:/usr/openwin/bin/: /usr/local/games:/usr/ccs/lib/:/usr/new:/usr/sbin/:/sbin/: /usr/openwin/lib:/usr/X11/bin:/usr/bin/X11/:/usr/local/bin/X11: /usr/bin/pbmplus:/usr/etc/:/usr/dt/bin/:/usr/lib: /usr/lib/nis:/usr/share/bin:/usr/share/bin/X11: /home/hware/work/cdk/main/cdk/../bin:.} LIBPATH: {/usr/lib/j2re1.3/lib/i386:/usr/lib/j2re1.3/lib/i386/native_threads: /usr/lib/j2re1.3/lib/i386/client:/usr/lib/j2sdk1.3/lib/i386:/usr/lib:/lib}
on my linux workstation. (java added all those except /lib and /usr/lib). But these two lines aren't the same on window either:
This system is windows nt
the current value of PATH is: {d:\OrbixWeb3.2\bin;D:\jdk1.3\bin;c:\depot\cdk\main\cdk\bin;c:\depot\ cdk\main\cdk\..\bin;d:\OrbixWeb3.2\bin;D:\Program Files\IBM\GSK\lib;H:\pvcs65\VM\win32\bin;c:\cygnus \cygwin-b20\H-i586-cygwin32\bin;d:\cfn\bin;D:\orant\bin; C:\WINNT\system32;C:\WINNT; d:\Program Files\Symantec\pcAnywhere; C:\Program Files\Executive Software\DiskeeperServer\;} LIBPATH: {D:\jdk1.3\bin;.;C:\WINNT\System32;C:\WINNT;D:\jdk1.3\bin; c:\depot\cdk\main\cdk\bin;c:\depot\cdk\main\cdk\..\bin; d:\OrbixWeb3.2\bin;D:\Program Files\IBM\GSK\lib; H:\pvcs65\VM\win32\bin;c:\cygnus\cygwin-b20\H-i586-cygwin32\bin;d:\cfn\bin; D:\orant\bin;C:\WINNT\system32; C:\WINNT;C:\Program Files\Dell\OpenManage\ResolutionAssistant\Common\bin; d:\Program Files\Symantec\pcAnywhere; C:\Program Files\Executive Software\DiskeeperServer\;}
Java is prepending itself! That confused me--- and broke my exec from ant.
发表评论
-
maven3的superpom换地方了
2011-06-17 16:00 1333路径是: maven-model-builder-3.0.3 ... -
eclipse 文件本地历史比较功能丢失
2010-09-07 19:01 2161如题,原来是我把该功能关闭了,在“首选项-capabiliti ... -
hibernate criteria like expression for Long
2010-05-13 18:11 1442https://forum.hibernate.org/vie ... -
java.lang.ClassFormatError: Incompatible magic value 218762506 in class file
2010-05-07 19:12 5088http://bugs.sun.com/view_bug.do ... -
maven2 install-file 不能一条命令安装jar source javadoc
2010-03-19 16:56 1945http://old.nabble.com/Help-unde ... -
maven2 eclipse hibernate test 测试类查询不到数据
2010-01-27 13:21 1840使用maven2构建的eclipse工程,写的测试类在ecli ... -
hibernate tools oracle nvarchar2 被映射成Serializable
2010-01-15 18:29 2865http://opensource.atlassian.com ... -
maven2 eclipse buildpath resources 问题
2010-01-15 09:29 2014https://docs.sonatype.org/displ ... -
spring aop方式获取方法执行时间不准
2010-01-09 17:47 1181AspectJ不管采用哪种通知方式(Before ,After ... -
Criteria + Projection + LockMode causes a NPE
2010-01-05 17:20 1055http://opensource.atlassian.com ... -
Hibernate Criteria 关联查询
2009-12-31 16:40 1854http://www.blogjava.net/hilor/a ... -
jbpm-3.3.1.GA 在tomcat/jetty配置方法
2009-08-06 16:16 1195jbpm-3.3.1.GA 在tomcat/jetty配置方法 ... -
pojo ejb web Services
2008-07-30 21:33 1048ejb to web Services http://www- ... -
Java中 static/transient,final/volatile 说明
2008-07-03 11:14 1843http://hi.baidu.com/jxliaom/blo ... -
web服务器绑定0.0.0.0 和 127.0.0.1 的区别
2008-06-15 16:47 5488发现在局域网其他人的电脑上浏览我的电脑上的提供的WEB服务的时 ... -
怎样在Eclipse中使用debug调试程序?
2008-06-06 09:45 1666参见: http://hua6884858.iteye.co ... -
什么情况需要 if (log.isDebugEnabled()) {}
2008-05-07 18:34 8702在使用log4j,common-log这样的log框架时,发现 ... -
使用 CAS 在 Tomcat 中实现单点登录 - zt
2008-04-17 14:04 2978级别: 初级 张 涛 (zzhangt@cn.ibm.com) ... -
对多核环境中多线程(JVM)与多进程(LAMP)的比较 - zt
2008-04-17 10:13 6052PHP 3之后的主要语言开发者之一、Zend公司的创始人之一A ... -
什么是ETag - zt
2008-04-01 10:11 1235使用ETags减少Web应用带宽和负载http://www.i ...
相关推荐
This application note is a step by step guide for setting up an easy FDX client application in C# .NET that interfaces to CANoe via FDX. CANoe FDX (Fast Data eXchange) is a UDP-based protocol for ...
First, let me thank you for taking the time to purchase and read my guide, “Mastering Java: An Effective Project-Based Approach including Web Development, Data Structures, GUI Programming and Object ...
When you change the mappings, you must calculate the size of the variables to be read from the PLC in order to use the correct Read Length parameter. The read length parameter is in transport size ...
**Delphi**, known for its rapid application development (RAD) capabilities, provides an excellent environment for creating graphical user interfaces (GUIs). **Python**, on the other hand, has become a...
We are often asked if it is possible to run an UltraEdit macro or script on a file from the command line. The answer is yes - and it's not only possible, it's extremely simple! Using find/replace ...
Tools/Recall Statement: Directory name now supports environment variables General IDE enhancements Full screen mode will now show minimized docking tools so that they can still be accessed ...
Visual Basic .NET is an extremely rich programming environment, and I’ve had to choose between superficial coverage of many topics and in-depth coverage of fewer topics. To make room for more topics...
You'll learn how to read from and write to files, manage directories, and handle file permissions. **Logic of Object-Oriented Programming (OOP)** - **Classes and Objects:** OOP revolves around the ...
17)..Added: Support for relative file paths and environment variables for events and various module paths 18)..Added: Logging in Manage tool 19)..Added: Windows 10 version detection 20)..Added: Stack ...
- **JDeveloper**: An integrated development environment (IDE) provided by Oracle for building applications using Java and PL/SQL. This includes creating, editing, testing, and debugging PL/SQL code. ...
ask - get environment variables from stdin base - print or set address offset bdinfo - print Board Info structure bmp - manipulate BMP image data boot - boot default, i.e., run 'bootcmd' bootd - boot ...
* Allow user to specify an alternate configuration file in Environment Options (still can be overriden by using "-c" command line parameter). * Lots of bug fixes. Version 4.9.8.1 * When creating a ...
The user address space is where application code, global variables, per-thread stacks, and DLL code would reside. The system address space is where the kernel, executive, HAL, boot drivers, page ...
- **Xcode Environment**: Xcode is the integrated development environment (IDE) used for developing iOS applications. It includes tools for editing code, building apps, debugging, and managing ...
Work with a combination of access modifiers, prefixes, properties, fields, attributes, and local variables to encapsulate and hide data Master DOM manipulation, cross-browser strategies, and ES6 ...
The PHPRC environment variable. (As of PHP 5.2.0) ; 3. A number of predefined registry keys on Windows (As of PHP 5.2.0) ; 4. Current working directory (except CLI) ; 5. The web server's directory ...