Hi, I'm trying to make a very simple java tester program. So, my program asks the user for the name of a method, and then runs it (doing a bunch of tests in the process) Does anyone know the name of the method(s) that will allow me to do this? Or even better, is there anyway I can get user input, and covert it directly into Java?

For example, the user inputs System.out.println("hello world");
and the program would actually run it?

Recommended Answers

All 3 Replies

Look for examples of reflection on how to do this. Many of the methods inside of the class named "Class" will help you do this.

In general, you will need to parse a string like "System.out.println" and look up System... then look up out... then look up the method println. etc. This can all be done with reflection.

Reflection is your friend.

But a better way by far would be to use JUnit (http://www.junit.org) as a testing framework.
It supports complete automation, you effectively write test scripts (as Java classes) and JUnit executes then and shows the results.
You can even integrate it with Ant (http://ant.apache.org) to provide fully automated testing as part of your build process.

Hi everyone,

You can use the java.exe together with your java runtime class. Its a bit old school but its effective and works like a charm. You can actually have a file dialog where the user chooses the class file on the disk and you can use the runtime class to run it. You can also use class loaders.

As for the testing of the class you can add methods in it to test it or read specific methods in it for testing

I hope this helped you

Yours Sincerely

Richard West

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.