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


#1

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?


#2

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:


#3

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.


#4

Thanks! It works! :+1: