Hi,

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.

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

never mind, I got it:

new Thread((Runnable) Class.forName("testpackage.Tests."+className).getConstructor(String.class).newInstance(outPutInClass)).start();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.