4

To obtain method parameters names using java reflection we have to compile the Java class with "-parameters" option, but when I do it from eclipse using VM Arguments, I get the following error in console.

Unrecognized option: -parameters
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

I have Java 8 running in my system. What am I missing?

greg-449
  • 109,219
  • 232
  • 102
  • 145
pavi
  • 654
  • 1
  • 9
  • 29
  • 2
    parameter is not a JVM option. To store formal parameter names in a particular .class file, and thus enable the Reflection API to retrieve formal parameter names, compile the source file with the -parameters option to the javac compiler. – Hector Jul 05 '16 at 08:26
  • @Hector Can you please explain how I can achieve that in eclipse? – pavi Jul 05 '16 at 08:31
  • 2
    Select the menu: Window > Preferences > Java > Compiler There you will find several check boxes under the heading "Classfile Generation". – Hector Jul 05 '16 at 08:33

1 Answers1

12

The problem is that this is not a VM argument. i.e.; It's not meant to be used with java, but rather with javac.

In eclipse, you can go to:

  • Window > Preferences > Java > Compiler , or to
  • Project > Properties > Java Compiler

And there is a check box to enable "Store information about method parameters (usable via reflection)." That you can use for this.

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • Thanks for the reply, it works. I also want to mention that as Method.getParameters() is available only from java8, the above mentioned check box i.e "Store information about method parameters (usable via reflection)." is available in neon. But I'm not sure if eclipse had this check box in any previous versions but the java must be java 8 onwards. – pavi Jul 05 '16 at 09:31
  • 1
    I'm using Mars.2 myself, which only requires Java 1.7 install. I guess they added that after 1.8 came out. So it might even be there in earlier versions. PS. I checked, it's also available in Luna. – Jorn Vernee Jul 05 '16 at 09:40
  • 1
    But the method Method.getParameters() is available only from java 1.8. Check the doc https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Executable.html#getParameters--. – pavi Jul 06 '16 at 06:51