cancel
Showing results for 
Search instead for 
Did you mean: 

Class reference in Eclipse project

Former Member
0 Kudos

Hi guys,

I want to directly point a java class in a main method and build a new instance of the called class.

In order to understand the class I use the following code:

_____________

String jMapClass = args[0];

System.out.println(Class.forName(jMapClass));

___________________

nevertheless I get the following error:

java.lang.ClassNotFoundException

I want to give class as input to the main.

So i add the class to the project and i give it with fully qualified name ad argument to the main.

for example package is

/javamappingtester/javamappingtester

path of the class to load is

/javamappingtester/javamappingtester/ParsingJAXPOAL.java(input of the arguments without ".java")

path of the main is

/javamappingtester/javamappingtester/Tester.java

what is wrong?

any suggestion?

thanks

regards,

Giamma

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

The issue was just some helper class not connected in the class path

Former Member
0 Kudos

Hi Giamma,

I tried creating the same code that you want, and it worked fine. I think the following code should help you, it creates a class object given the type of object name as a string arguement.


Object object = null;
Class classDefinition;
try {
	classDefinition = Class.forName("com.guru.utils.TestClass");
	object = classDefinition.<b>newInstance</b>();
	System.out.println(object);
} catch (ClassNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (InstantiationException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IllegalAccessException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

Best regards,

Guru.