1

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

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
Jakir00
  • 2,023
  • 4
  • 20
  • 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,250
  • 7
  • 45
  • 84
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
  • 63,018
  • 25
  • 139
  • 189