Search code examples
javabashcygwinclasspath

How to set Java classpath in bash script used in Ubuntu and Windows (Cygwin)


I am writing a bash script that should execute some Java application that needs a specific classpath.

Further, this script should be executable on both, Ubuntu & Windows (Cygwin).

The problem: The seperator on Windows is ";" and the seperator on Ubuntu is ":". This results in java -cp A.jar;B.jar Main on Windows (also when using cygwin, because it's using Windows' java) and java -cp A.jar:B.jar Main on Ubuntu.

The question: How to detect in the bash script which underlying operating system is running / which java classpath seperator to use?


Solution

  • A naive (but quick to implement) approach: run uname -a first and push that into a variable. Then check if that output contains "Ubuntu".

    If so, you should be using the ":" as separator; otherwise ";" should do.

    And if you want to be prepared for other platforms in the future; instead of doing a simple if/else; you might want to check what uname -a has to say on cygwin; to setup some $SEPARATOR_CHAR variable using a bash switch statement.