Illegal reflective access operation warning occurs when using java.net.URL setRequestMethod

Hi!

My script creates a simple GET request:

#!/usr/bin/env clj

(import java.net.URL)

(let [connection (. (new URL "http://dummy.restapiexample.com/api/v1/employees") openConnection)]
  (. connection setRequestMethod "GET")
  (let [body (slurp (. connection getInputStream))]
    (println body)))

It works but it yields warnings on startup:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by clojure.lang.InjectedInvoker/1384454980 (file:/Users/user/.m2/repository/org/clojure/clojure/1.10.0/clojure-1.10.0.jar) to method sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(java.lang.String)
WARNING: Please consider reporting this to the maintainers of clojure.lang.InjectedInvoker/1384454980
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

The warning disappears if I just remove setRequestMethod. However, if I need to create a POST request later I still need to use setRequestMethod. How can I use setRequestMethod without getting these warnings?

Just so you don’t get too worried, you safely ignore these warnings for some period of time. The code keeps working. It’s just that after Java 9 some things are more strict.

Unfortunately, I’m also clueless on how to fix this issue :frowning:

1 Like

You can get rid of the warning by using ^HttpURLConnection connection and (import java.net.HttpURLConnection). Reflection is just picking a class that it isn’t “allowed” to technically.

3 Likes

Thanks! It works! :+1:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.