title: In Java, how can I load classes without knowing the name ahead of time?
body: <p>I have a directory that looks like this:</p>
  
  <pre><code>src
  |
  |__ProblemTester.java
  |
  |__problems
     |
     |__AbstractProblem.java
     |
     |__Problem1.java
     |
     |__Problem2.java
     |
     |__Problem3.java
  </code></pre>
  
  <p>All of the classes extend <code>AbstractProblem</code>. I want to have a function that takes the input <code>int n</code> and returns an instance of <code>ProblemN.java</code> (as part of a testing framework). Is this possible?</p>
  
  <p>My code so far: (very rough)</p>
  
  <pre><code>class ProblemTester {
  
      Class&lt;? extends AbstractProblem&gt; problem;
  
      ProblemTest(int number) throws IllegalArgumentException {
          try {
              problem = Class.forName("problems.Problem" + number);
          } catch (ClassNotFoundException e) {
               throw new IllegalArgumentException(String.format("Problem %d not found.", number));
          }
      }
  </code></pre>
  
  <p>Unfortunately this throws the error <code>Incompatible types. Required: Class&lt;? extends AbstractProblem&gt;. Found: Class&lt;capture &lt;?&gt;&gt;.</code></p>
  
  <p>How can I fix this? Is there an existing framework for something like this? I've used gradle in the past but I don't think it loads classes automatically like I'd like this function to do. Thanks.</p>