• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class.forName path in Eclipse IDE  RSS feed

 
Nenad Bulatovic
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I have a problem with forming path in Eclipse IDE for Class.forName.
Here is sample of code (just example code from book for Java 6), how should I for path to make it work within IDE (none of those I tried worked)?

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

None of lines 13-16 is correct, IDE or no IDE, for two different reasons. The javadocs of the Class.forName method show how fully-qualified class names -a class name including the package name- (which is what that is, not a file name) have to be specified.
 
Nenad Bulatovic
Greenhorn
Posts: 24
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Welcome to JavaRanch.

None of lines 13-16 is correct, IDE or no IDE, for two different reasons. The javadocs of the Class.forName method show how fully-qualified class names -a class name including the package name- (which is what that is, not a file name) have to be specified.


I got it! Thanks.

This will do the job:

 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nenad Bulatovic wrote: . . .
This will do the job:

Only if you add newInstance()
You can read more about newInstance in this other current thread.
 
Nenad Bulatovic
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what you mean by that.
I just added that line



instead of



in the above code and it worked.

Actually according to the book it seems that forName automatically do it :

3. What Class method is used to create a new Class object using a string containing
the name of a class?
a. newInstance()
b. forName() <---- This is correct
c. getName()
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Only if you add newInstance()

No, a Class object is needed here, not an instance of that class; see line 9 of the code.
 
Nenad Bulatovic
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
Campbell Ritchie wrote:Only if you add newInstance()

No, a Class object is needed here, not an instance of that class; see line 9 of the code.


I don't understand - here is explanation from the book what should that code do:

The MethodInspector application displays information about the public methods in the
class you specify at the command line (or MethodInspector itself, if you don’t specify a
class).


And the output from that app is:
Method: main()
Modifiers: public static
Return Type: void
Parameters: [Ljava.lang.String;


I think it does what it is supposed to do? Or not?
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote: . . .
No, a Class object is needed here, not an instance of that class; see line 9 of the code.
Sorry. I read that wrongly
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nenad Bulatovic wrote:I don't understand

I merely pointed out that Campbell misread your problem, so his response is misleading. If the code works for you, then all is good. :-)
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did misread it. Sorry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!