Is there a way for a java program (or more specifically java in jsp) to find out which platform (windows or linu开发者_如何转开发x) it is currently running in? Thanks in advance.
String os = System.getProperty("os.name");
The question is what you need this for. The usual approach is asking on the values of system properties, which are not well defined, and may break if you for some reason need to choose another JVM or another platform.
Another approach is actually testing the underlying platform for the functionality you need to know how works during startup. I.e. if you want to know if you can remove a file that is open or not, then write a small snippet testing for that. If you want to know if the file system is case sensitive, then write a snippet testing for that.
This will make your code more robust in the long run.
If you do want to use System.getProperty("os.name")
to identify the environment, then you may be interested in a list of identifiers used by the company formerly known as SUN
Try:
System.getProperty("os.name")
You may also find this function useful.
System.getProperty("os.name")
For other similar properties and their descriptions, see java.lang.System
精彩评论