views:

130

answers:

5

Is there a way for a java program (or more specifically java in jsp) to find out which platform (windows or linux) it is currently running in? Thanks in advance.

+3  A: 

Try:

System.getProperty("os.name")

You may also find this function useful.

Darin Dimitrov
+6  A: 
String os = System.getProperty("os.name");
InsertNickHere
+1  A: 
System.getProperty("os.name")

For other similar properties and their descriptions, see java.lang.System

Bozho
+6  A: 

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.

Thorbjørn Ravn Andersen
+1, agree - if we really need os-dependant behaviour then we should consider building specific applications for different environments.
Andreas_D
@Andreas, that might be overdoing it, if all you need is a few flags.
Thorbjørn Ravn Andersen
Thanks guys. I was just using it to set a proper location for the data directory. Your advice is much more superior, I will keep that in mind. Thanks.
Kenneth
+3  A: 

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

Andreas_D
Thanks. The list is useful.
Kenneth