GetConstructor () without parameters in java

I cannot use getConstructor() for a constructor without parameters. I keep getting

 java.lang.NoSuchMethodException: classname.<init>() 

here is the code

 import java.lang.reflect.*; class asa implements InfoInterface{ public String getClassName() { return("jeden"); } public String getMethodName() { return("metoda"); } public String getArgument() { return("krzyk"); } } class jeden { jeden() { System.out.println("konstruktor"); } public void Metoda(String s) { System.out.println(s); } } class Start { public static void main( String[] argv ) { String klasa, metoda, argument; InfoInterface d; if ( argv.length == 0 ) { System.err.println( "Uzycie programu: java Start nazwa_klasy nazwa_klasy2..."); return; } try { for(int x=0;x<argv.length;x++) { Class c = Class.forName( argv[x] ); d=(InfoInterface)c.newInstance(); klasa = d.getClassName(); metoda = d.getMethodName(); argument = d.getArgument(); Class<?> o = Class.forName(klasa); //o.newInstance(); Constructor oCon=o.getConstructor(); System.out.println("ASD"); Class p =(Class) oCon.newInstance(); } } catch ( Exception e ) { System.out.println(e) ;} } } 

o.newInstance (); prints "constructor" without any problems

+5
source share
3 answers

The problem is clear when you read javadoc .getConstructor() :

Returns a constructor object that reflects the specified constructor of the public class represented by this class object.

Emphasis is mine.

In your code, the constructor is not public!

Example:

 // Note: class is NOT public -- its default constructor won't be either final class Test { public static void main(final String... args) throws NoSuchMethodException { // throws NoSuchMethodException Test.class.getConstructor(); } } 

A mandatory link to the SO> response, which also gives a link to JLS. In particular, note that the default constructor has the same access modifier as the class.

+14
source

It seems that your class provides a constructor that is NOT the default constructor. Calling getConstructor () without parameters requires the class to have a default constructor. The following example illustrates this.

 import org.junit.Test; public class ConstructorTest { public static class ClassWithParameterizedConstructor { public ClassWithParameterizedConstructor(final String param) { // A parameterized constructor, no default constructor exists } } @Test public void testFoo() throws NoSuchMethodException { // Parameterized constructor lookup works fine ClassWithParameterizedConstructor.class.getConstructor(String.class); // This doesn't work since there is no default constructor ClassWithParameterizedConstructor.class.getConstructor(); } } 

Thus, a possible solution is to either change the call to getConstructor () to include the correct type, or provide a default constructor for the object itself (but why are you doing this?).

+2
source

Read this: http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html

It seems that both the Class and Constructor classes have a newInstance method, the difference is that in the Class class you can call newInstance without arguments, so the called constructor should not have any arguments (this also causes a problem if you have more than that one constructor). The methone newInstance method in the Constructor class also allows you to call the constructor with arguments, you can also use the getConstructors method instead of getConstructor, which returns all the class constructors and allows you to call the constructor method that you want.

In this case, since you only have one constructor with no arguments, Class.newInstance works fine. To use getConstructor to get the same result, you need to add oCon.newInstance ();

0
source

Source: https://habr.com/ru/post/1209600/


All Articles