Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default Find the running .jar's filename?

    I worked out how to find it's DIR
    Java Code:
    Main.class.getProtectionDomain().getCodeSource().getLocation().getPath()
    But how do I find it's filename?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Why do you want to know that? It could be me but I don't see any use for it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Why do you want to know that? It could be me but I don't see any use for it.

    kind regards,

    Jos
    I just do, for a program I've half done making. So, how do I do it?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AutomaticCoding View Post
    I just do, for a program I've half done making. So, how do I do it?
    I don't know but you didn't answer my question.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I don't know but you didn't answer my question.

    kind regards,

    Jos
    Because I'd rather not as it's probably frowned upon on this forum.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Have you read the API for the classes returned by those methods? No? Why not?

    db

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AutomaticCoding View Post
    Because I'd rather not as it's probably frowned upon on this forum.
    I still don't know why you want to know the location (file) where your class came from; it smells like bad design, but I just checked the API docs and this should be a way to do it:

    Java Code:
    Class c= Class.forName("your.complete.class.name"); // or any other way to obtain a Class object
    ProtectionDomain pd= c.getProtectionDomain();
    CodeSource cs= pd.getCodeSource();
    		
    System.out.println(cs.getLocation());
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Output Jar Filename
    By CuddlyKittens11 in forum NetBeans
    Replies: 0
    Last Post: 04-25-2011, 01:09 AM
  2. filename as a string
    By Saran185 in forum New To Java
    Replies: 6
    Last Post: 02-13-2011, 01:19 AM
  3. cannot find the class file when running the java command
    By kulangotski in forum New To Java
    Replies: 4
    Last Post: 01-18-2011, 11:34 AM
  4. New filename after try catch
    By hellzxmaker in forum New To Java
    Replies: 3
    Last Post: 11-05-2010, 11:07 PM
  5. Replies: 1
    Last Post: 06-30-2009, 02:32 PM

Posting Permissions

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