-
July 21st, 2010, 06:55 AM
#1
problem with the path with Class.forname() method
Hello,
I wrote this and it doens't find the proper path:
Code:
String userDir = System.getProperty("user.dir");
Class c = Class.forName(userDir + "\\" + "Page");
either:
Class c = Class.forName("Page");
Page.class is located in the same dir returned by "System.getProperty("user.dir");"
The tree of directory is:
Code:
Myproject\Page.class
Myproject\bin\.....
Myproject\src\.....
etc. etc.
I tried (using "Class c = Class.forName("Page");" ) to move Page.class in the \bin directory using of my project and it works! So I guess is a path problem!
But what's the problem?
thanks
-
July 21st, 2010, 07:12 AM
#2
Re: problem with the path with Class.forname() method
run
Class c = Class.forName(userDir.replace("\\", ".") + "." + "Page");
if this doesn't work, user dir may have something funny at the start of the string (i.e. c:\\), if it does just strip it out.
-
July 21st, 2010, 07:19 AM
#3
Re: problem with the path with Class.forname() method
Originally Posted by v.z.afzal@dundee.ac.uk
run
Class c = Class.forName(userDir.replace("\\", ".") + "." + "Page");
if this doesn't work, user dir may have something funny at the start of the string (i.e. c:\\), if it does just strip it out.
No it doens't work. I don't understand your suggestion by the way. No funny characters in userDir BTW.....
-
July 21st, 2010, 08:11 AM
#4
Re: problem with the path with Class.forname() method
The class loader looks for classes on the classpath. If your class isn't in a package (doesn't have a 'package' declaration at the top), then you only need to pass the class name to the Class.forName method. If class Page is in the user directory (why?), then you need to put the user directory into the classpath so the loader can find it.
See Setting The Classpath.
The unavoidable price of reliability is simplicity...
C.A.R. Hoare
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 21st, 2010, 08:54 AM
#5
Re: problem with the path with Class.forname() method
Originally Posted by dlorde
The class loader looks for classes on the classpath. If your class isn't in a package (doesn't have a 'package' declaration at the top), then you only need to pass the class name to the Class.forName method. If class Page is in the user directory (why?), then you need to put the user directory into the classpath so the loader can find it.
See Setting The Classpath.
The unavoidable price of reliability is simplicity...
C.A.R. Hoare
THe .class is in the user path because I've compiled it by JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); and I put it there (my choice). It doens't belong to any package. In fact , as said, if I put it inside \bin directory no problems at all! I've added userDir sa said but don't work. Isn't strange?
-
July 22nd, 2010, 04:43 AM
#6
Re: problem with the path with Class.forname() method
Originally Posted by mickey0
THe .class is in the user path because I've compiled it by JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); and I put it there (my choice). It doens't belong to any package.
It doesn't matter how you compiled it. If it doesn't belong to a package, you pass only the class name to the classForName method.
In fact , as said, if I put it inside \bin directory no problems at all! I've added userDir sa said but don't work. Isn't strange?
If if works in one directory but not in another, then one directory is on the classpath and the other isn't.
The problem is never how to get new, innovative thoughts into your mind, but how to get old ones out!
D. Hock
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|