1

I've just installed Kafka following tutorial. It doesn't start because of sh script error:

$ sudo kafka-server-start.sh /etc/kafka.properties /opt/Kafka/kafka_2.12-1.1.0/bin/kafka-run-class.sh: line 252: [[: 10 2018-04-17: syntax error in expression (error token is "2018-04-17") [0.000s][warning][gc] -Xloggc is deprecated. Will use -Xlog:gc:/opt/Kafka/kafka_2.12-1.1.0/bin/../logs/kafkaServer-gc.log instead. Unrecognized VM option 'PrintGCDateStamps' Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. 

checking line 252:

if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then 

I've added echo to get more info:

JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([^.-]*).*"/\1/p') echo "JAVA_MAJOR_VERSION: $JAVA_MAJOR_VERSION" if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then 

output is changed with:

JAVA_MAJOR_VERSION: 10 2018-04-17

my java:

$ java -version openjdk version "10.0.1" 2018-04-17 

Question

How I should change JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([^.-]*).*"/\1/p') to fix my Kafka starter?

4
  • When did Java jump from v1.x to v10.x? OpenJDK on my host is still reporting openjdk version "1.8.0_171". Notably, without a date after the version number in the output.
    – DopeGhoti
    CommentedJul 17, 2018 at 20:35
  • 1
    Probable simple change, add | cut -d' ' -f1 after the sed command. Others can magic sed itself. Interesting that openjdk added additional information to the version line. As @DopeGhoti noted, 1.8 doesn't have the date on it. The release of Java 10, though, was early this year, so I'd expect some distributions to start picking it up.
    – KevinO
    CommentedJul 17, 2018 at 20:39
  • @KevinO, please add answer, to make me vote it. You are great - thanks.
    – Serhii
    CommentedJul 17, 2018 at 20:49
  • My version of this using Bash expansions is here stackoverflow.com/a/77210970/430139
    – ivenhov
    CommentedOct 1, 2023 at 17:21

2 Answers 2

2

This works for older "1.x.y" format as well as newer "x.y" format:

java -version 2>&1 \ | head -1 \ | cut -d'"' -f2 \ | sed 's/^1\.//' \ | cut -d'.' -f1 

Source: https://odoepner.wordpress.com/2014/07/27/get-java-version-string-via-shell-commands/

    1

    One solution is to drop the date off the end by using cut. This approach would result in:

    JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([^.-]*).*"/\1/p' | cut -d' ' -f1) 

    This approach splits on the space character (-d' ') and takes the first field. Based upon the input, that would drop the openjdk appended date.

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.