1

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

Johannes Schaub - litb
  • 481,675
  • 123
  • 870
  • 1,191
Jakir00
  • 1,973
  • 3
  • 19
  • 32

2 Answers2

1

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.

Community
  • 1
  • 1
FeifanZ
  • 16,108
  • 6
  • 46
  • 80
1

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()
Lawrence Dol
  • 61,437
  • 25
  • 136
  • 186