CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    273

    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

  2. #2
    Join Date
    Jul 2010
    Posts
    17

    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.

  3. #3
    Join Date
    Jul 2007
    Posts
    273

    Re: problem with the path with Class.forname() method

    Quote Originally Posted by v.z.afzal@dundee.ac.uk View Post
    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.....

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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.

  5. #5
    Join Date
    Jul 2007
    Posts
    273

    Re: problem with the path with Class.forname() method

    Quote Originally Posted by dlorde View Post
    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?

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: problem with the path with Class.forname() method

    Quote Originally Posted by mickey0 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured