How to set Java class path in bash script used on Ubuntu and Windows (Cygwin)

I am writing a bash script that must execute some kind of Java application that requires a specific class path.

In addition, this script must be run on both Ubuntu and Windows (Cygwin).

Problem: The separator on Windows is " ; ", and the separator on Ubuntu is " : ". This results java -cp A.jar;B.jar Mainin Windows (also when using cygwin because it uses java for Windows) and java -cp A.jar:B.jar Mainon Ubuntu.

Question: How to determine in bash script which operating system is running / which java classpath seperator to use?

+4
source share
2 answers

A naive (but quickly implemented) approach: first run uname -aand push it into a variable. Then check if this output contains "Ubuntu".

If so, you should use the delimiter ":" as the delimiter; otherwise ";" must do.

And if you want to be prepared for other platforms in the future; instead of a simple simple if / else; you can check what uname -acygwin has to say; to set some variable $SEPARATOR_CHARusing the bash statement.

+2
source

For final decision only:

I just tested uname -son Ubuntu, Mac and Windows running cygwin and came up with the following script:

if [ "$(uname)" == "Darwin" ]; then
    # Do something under Mac OS X platform
    SEP=":"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
    # Do something under GNU/Linux platform
    SEP=":"
elif [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then
    # Do something under Windows NT platform
    SEP=";"
fi
0

Source: https://habr.com/ru/post/1657491/


All Articles