I am a PHP developer that has been put on a Java project and am trying to apply the same principles with new ones that Java provides and am stuck. I have successfully instantiated a class with a variable class name, which contains a constructer that has one parameter. What I am trying to do now is wrap this in a thread.

Let me give you an idea of the project I am working on. This is going to be a Selenium testing library for all of our internal and external applications and websites. This first portion loops through a configuration of tests(class names), instantiates them and runs them. What I want to do is insert this into a thread so that all of the tests will run simultaniously. Below is my code:

for (String className : TestConfig.getConfig("tests").split(" ")) {
            String outPutInClass = "firefox";
            Class<?> cl = Class.forName("testpackage.Tests."+className);
            Constructor<?> con = cl.getConstructor(new Class[] {String.class});
            Object xyz = con.newInstance(outPutInClass);

This code works fine but inserting it into a thread is proving to be a challenge.

2 Years
Discussion Span
Last Post by R0bb0b

This is what I am trying to do:

(new Thread(Class.forName("testpackage.Tests."+className).getConstructor(String.class).newInstance(parameterPassedToConstructor))).start();

And I am getting the error:
The constructor Thread(capture#1-of ?) is undefined

Edited by R0bb0b


never mind, I got it:

new Thread((Runnable) Class.forName("testpackage.Tests."+className).getConstructor(String.class).newInstance(outPutInClass)).start();
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.