1

I'm pretty new in groovy, trying to encode a URL in my first piece of groovy code.

Here is a section of my code:

import java.net.URLEncoder
String url = baseUrl + URLEncoder.encode(parameter);

It looks quite the same as many examples I check online but it throws error as below.

General error during canonicalization: Importing [java.net.URLEncoder] is not allowed

java.lang.SecurityException: Importing [java.net.URLEncoder] is not allowed

I have also tried to use the class directly as illustrated here

String url = baseUrl + java.net.URLEncoder.encode(parameter);

and another version:

String url = baseUrl + URLEncoder.encode(parameter);

both throw me error:

General error during canonicalization: Indirect import checks prevents usage of expression

java.lang.SecurityException: Indirect import checks prevents usage of expression

appreciate if any guru can help clear the doubts.

Community
  • 1
  • 1
Lee
  • 2,874
  • 3
  • 27
  • 51
  • Can you show us the whole code? I installed Groovy here and execute the following code: `println URLEncoder.encode("URL encoding fine!")`, it prints `URL+encoding+fine%21`. Moreover, using `import java.net.URLEncoder` or static import also works fine. – TulaGingerbread Jun 05 '14 at 08:51
  • What type and version of the JDK are you running? – bdkosher Jun 05 '14 at 14:36

1 Answers1

2

According to http://groovy.codehaus.org/Differences+from+Java, java.net.* package in Groovy is imported by default, which means java.net.URLEncoder is also imported. Use it without import.

Edit: For me, using this Groovy code:

println URLEncoder.encode("URL encoding fine!")

prints URL+encoding+fine%21

TulaGingerbread
  • 435
  • 5
  • 15
  • 1
    thanks. I have tried that as well earlier but also gives me "Indirect import checks prevents usage of expression" error. – Lee Jun 05 '14 at 08:34
  • Unfortunately I can't check this now, but I too found a lot of examples of using URLEncoder just as `URLEncoder.encode(string)`. – TulaGingerbread Jun 05 '14 at 08:40
  • Yeah. That's exactly what puzzles me. It doesn't look like a complex stuff. – Lee Jun 05 '14 at 09:06