How do I use Java preprocessors to determine what OS I am compiling on?

- 496,577
- 130
- 894
- 1,212

- 2,023
- 4
- 20
- 32
-
12Uh, what Java preprocessor? – James McNellis Jan 07 '11 at 02:31
-
@James: [spp](http://hg.openjdk.java.net/jdk6/jdk6/jdk/file/tip/make/tools/src/build/tools/spp/Spp.java), of course! ;-) – C. K. Young Dec 01 '11 at 22:56
2 Answers
Try this:
System.getProperty("os.name")
From How do I programmatically determine operating system in Java?
BTW Java doesn't have a preprocessor...one of the annoying things I discovered.
-
1
-
Hm...good point...but I guess I was thinking that if you're running the compiler, then chances are you'll know what OS the compiler is running on... – FeifanZ Jan 07 '11 at 02:46
There is no Java preprocessor and no ability to conditionally compile.
There is a very primitive debugging feature built in whereby the compile is allowed to delete conditional blocks with a constant condition which is false to facilitate elimination of debug code - but I don't recall if the spec requires or allows the code to be deleted from the compiled class.
static final boolean DEBUG=false;
...
if(Debugging.DEBUG) {
// some code which the compile may (or must?) eliminate
}
If you want to detect the O/S at runtime and do different things on different platforms, there are a number of system properties required in every JVM which are documented in System.getProperties()
. See the JavaDoc, relative it's installed or network location:
api/java/lang/System.html#getProperties()

- 63,018
- 25
- 139
- 189