Yusuf Soysal
Yusuf Soysal

Reputation: 177

Groovy Logging cannot find log variable

I'm new to groovy and this is my first groovy code.

I wrote a small groovy application in IntelliJ and everything is working fine within IDE. Then I wanted to test the code in shell. Here is my start script:

#!/bin/sh
#--------------+
# START SCRIPT |
#--------------+
#
CLASSPATH=$(JARS=("lib"/*.jar); IFS=:; echo "${JARS[*]}")
CLASSPATH=.:$CLASSPATH

/Users/myUsername/Documents/dev/groovy-2.1.3/bin/groovy -cp $CLASSPATH com/bla/Frequency.groovy

I get a couple of errors I couldn't figure out. Here are two of them:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, com/bla/Frequency.groovy: 219: The name log doesn't refer to a declared variable or class. The static scope requires that you declare variables before using them. If the variable should have been a class check the spelling.
@ line 219, column 9.
       log.info "Application is completed!"

I actually annotated my class with @Slf4j annotation:

@Slf4j
class Frequency {
  public static void main(String[] args) {
    // do some things

    log.info "Application is completed!"
  }
}

Another problem is with multiple assignments. Here is my sample code:

def (elementId, elementValue) = equalsPattern.split(element)

Here is the error I got:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, /Users/myUsername/Documents/dev/workspace/bla/trunk/src/main/groovy/com/bla/Frequency.groovy: 163: unexpected token: def @ line 163, column 13.
           def (elementId, elementValue) = equalsPattern.split(element)

Upvotes: 0

Views: 1100

Answers (2)

Yusuf Soysal
Yusuf Soysal

Reputation: 177

My friend and I fixed the problem. We add all groovy jars to classpath while running the script and mystery problems are gone. If you can tell me why this solves the problem, I would appreciate it.

#!/bin/sh
#--------------+
# START SCRIPT |
#--------------+
#
GROOVY_HOME=/Users/yusufsoysal/Documents/dev/groovy-2.1.3

CLASSPATH=$(JARS=($GROOVY_HOME/lib/*.jar); IFS=:; echo "${JARS[*]}")
CLASSPATH=$CLASSPATH:$(JARS=("lib"/*.jar); IFS=:; echo "${JARS[*]}")
CLASSPATH=.:$CLASSPATH

$GROOVY_HOME/bin/groovy -cp $CLASSPATH com/bla/Frequency.groovy

Upvotes: 0

ataylor
ataylor

Reputation: 66109

I can't reproduce that error with the code you've posted. This works for me:

@Grab(group='org.slf4j', module='slf4j-simple', version='1.7.5')

@groovy.util.logging.Slf4j
class Frequency {
    public static void main(String[] args) {
        // do some things

        log.info "Application is completed!"
    }
}

Regarding the second issue: when you get an error message "unexpected token: def", it means that the parser wasn't able to understand the code above the error. The line you posted is correct.

Upvotes: 1

Related Questions