Results 1 to 5 of 5
  1. #1
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default trying to get class name & path

    I need to get the name and path of some of my classes and am having an issue.
    this code looks like it should work but give me an out put of:
    /////////////////////.class
    Java Code:
    package test;
    
    public class TestingClassName {
       public static void main(String[] args) 
      {
        System.out.println(
        TestingClassName.class.getName().replaceAll(".", "/") + ".class");
       }
    }

  2. #2
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    what version of java are you using ?Are you using 5.0 or higher?
    YOu can change you code to this:
    TestingClassName.class.getName().replaceAll("\\.", "/") + ".class");
    and escaping it with a "\\". In regex "." means replace everything so that is why all characters are replaced with "\". you also need to escape the "\" which is why I put in two.
    If you are using java 5.0 there is a new class you can use:java.util.regex.Pattern.quote

    But not all file systems use the "/" so you need to be careful with this.

  3. #3
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default

    Thanks for the help. What would I use if the system does not use a slash?

  4. #4
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    If you use 5.0 do this:
    Java Code:
     TestingClassName.class.getName().replaceAll("\\.", Matcher.quoteReplacement(File.separator)) + ".class");
    If you are using an earlier version use the String replace I guess.

  5. #5
    g25451 is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Setting up class path for Eclipse
    By tsantana in forum Eclipse
    Replies: 1
    Last Post: 04-07-2008, 08:05 PM
  2. Specify path
    By borgrodrick in forum Eclipse
    Replies: 1
    Last Post: 02-13-2008, 12:03 AM
  3. Log4J.jar in class path
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-02-2008, 07:39 PM
  4. problem with PATH
    By creativehacker in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 03:54 PM
  5. Specifying absolute path in web.xml
    By Felissa in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-05-2007, 07:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •