Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like your problem is here:</p> <pre><code> Class&lt;?&gt; c= klass.forName("Foo"); Method method = c.getDeclaredMethod ("getNome", String.class); System.out.println(method.invoke(c)); </code></pre> <p><code>klass.forName("Foo")</code> is actually equivalent to <code>Class.forName("Foo")</code>, which results in <code>Class.forName("Foo", nclass.class.getClassLoader());</code>.</p> <p>The class loader which loaded <code>nclass</code> obviously does not know a <code>Foo</code> class, since it was created by your anonymous class loader (which is a child of this class loader). So, don't use this <code>forName</code> call here, but simply use your <code>klass</code> object to get the method and invoke it.</p> <hr> <p>And of course, invoking and retrieving a method works not like you did.</p> <ul> <li><p>The <code>getMethod</code> and <code>getDeclaredMethod</code> take beside the name a list of argument types (not return types) - in your case <code>getNome</code> has no arguments, so it should be:</p> <pre><code>Method method = klass.getDeclaredMethod ("getNome"); </code></pre></li> <li><p>the invoke methods first argument is an object of the methods receiving type (<code>Foo</code> in your case), or <code>null</code> for static methods. The following arguments are the parameters to the method (i.e. none in your case). So you should use here:</p> <pre><code>System.out.println(method.invoke(null)); </code></pre> <p>It may be that the argument is simply ignored in your case, so <code>c</code> might not get an error. But there still is no reason to use a class object here, if you are not actually invoking a method of class <code>Class</code> by reflection.</p></li> </ul> <p>This all is assuming your error occurs in your <code>forName</code> call and not already earlier. Please learn to describe your error message, so we don't have to guess.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload