Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default Get Application Name in Code

    I can do it easily in other languages, but I have no idea how to write Java code that gets the name of the application.

    Thanks,
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    how to write Java code that gets the name of the application
    What application? Are you looking for the name of a class or of the jar file the class is loaded from or what?

  3. #3
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    I create Windows applications. In Java this app is a jar file. I can double-click the jar file in Windows Explorer and run the application. At the command prompt I type java -jar MyApplication.jar

    So I need to get the name of the jar file programatically.

    Thanks,
    Jon
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Why?
    My jar files are rarely if ever the proper name of the application.

    Hell, even windows apps are rarely the name of the application...the exes are usually some archaic abbreviation.

    Store a property file in the jar that contains a an appname property and read that.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    I need to get the name of the jar file programatically.
    Can you explain why you need the name?
    Tolls method fails if the jar file has been renamed.
    Which name would you want: the current name or the name given to jar file when it was created?

    If you want the current name try something like the following. This was copied from an old program
    URL ourLoc = obj.getClass().getResource(className);

    // My code showed this Output when load from a jar:
    //JAVA >Loaded from jar:file:/C:/My_Photos/SlideShowApp.jar!/ImgIdxEditor.class,

  6. #6
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    I don't care at all about the "proper" name of the app. I want the name of the file, whether it is an exe or a jar. In other apps I have written, that was easy to get. I use that name to serve as the name of other files the app writes, with different extensions, of course. I do not want to hard code the name in the app code itself, or in a property file.

    Thanks,
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Quote Originally Posted by Norm View Post
    Can you explain why you need the name?
    Tolls method fails if the jar file has been renamed.
    No it doesn't.
    It only fails if the application name changes.

    In my world jar-file-name != application-name.
    :)

    To the OP: Why do you want to tie it to the jar file? That really does not make any sense. It is just as much hardcode as a jar file name as it would be using a property file. And a property file is the normal way of going about doing this.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    Why do you want to tie it to the jar file?
    It could be used as a prefix to aux files the app might create.
    Each differently named jar file would have its own set of files.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    But if it's an app thing then have an app name?
    Using a jar-file name just strikes me as odd.

    I just don't see the gain at all in that.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    Have to wait for the OP to explain his requirements.

  11. #11
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    What is your application called?
    You could make different Strings for different languages and then use them.

  12. #12
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    It could be used as a prefix to aux files the app might create.
    Each differently named jar file would have its own set of files.
    You nailed it.
    Have to wait for the OP to explain his requirements.
    I did that in post #5.
    And... In my various apps where I need it, I have a standard routine that captures the file name and creates the various derived file names. I don't have to change a thing in that code or in a property file since it gets the appropriate name programatically.

    Now that some of my apps need to be in Java, I'm trying to do the same thing.

    Thanks,
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Except you're going to have to write code to get the jar file name (something I am not convinced is doable anyway)...so why not simply use existing properties functionality and go with that instead?

    I know why you want a name, but there is no reason that name has to be the jar-file name.

  14. #14
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    Except you're going to have to write code to get the jar file name
    Yes, but only one time.
    so why not simply use existing properties functionality
    I only want to do it once.
    but there is no reason that name has to be the jar-file name
    In my environment, there is a reason, which I won't go into. If not the jar-file name, then some other name unique to the jar-file, programatically obtainable, and not hardcoded in the app or in an external file. BTW, I name my properties file the same as the jar file, and my app can't find it unless it knows the name.
    Jon Jacobs
    This message was composed entirely from recycled electrons.

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

    Default

    Quote Originally Posted by JonJacobs View Post
    I don't care at all about the "proper" name of the app. I want the name of the file, whether it is an exe or a jar. In other apps I have written, that was easy to get. I use that name to serve as the name of other files the app writes, with different extensions, of course. I do not want to hard code the name in the app code itself, or in a property file.
    Even in C or C++ there's no guarantee that argv[0] represents the name of the executable file (check the ANSI/ISO specification). I don't find it a bad thing to store the name of your application as an external resource. That way you have a guarantee that you definitely have an application name.

    kind regards,

    Jos

  16. #16
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    Even in C or C++ there's no guarantee that argv[0] represents
    In Delphi, and C# I can get a guaranteed name of the executable.
    Jon Jacobs
    This message was composed entirely from recycled electrons.

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

    Default

    Quote Originally Posted by JonJacobs View Post
    In Delphi, and C# I can get a guaranteed name of the executable.
    Sure, both run only on a MS Windows platform and maybe a bit wobbly on Linux machines. Why not use one 'anchor' resource that defines the name of your application?

    kind regards,

    Jos

  18. #18
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    Sure, both run only on a MS Windows platform
    And that is the only platform I am writing these apps for/on.
    Jon Jacobs
    This message was composed entirely from recycled electrons.

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

    Default

    Quote Originally Posted by JonJacobs View Post
    And that is the only platform I am writing these apps for/on.
    OK, but Java does not give you the name of the executable .jar or .class file so why insist on doing it that way? It is not the way the JVM was defined.

    kind regards,

    Jos

  20. #20
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    OK, but Java does not give you the name of the executable .jar or .class file
    If that's the case, that's what I needed to know in the first place. None of the other discussion mattered.

    I guess that's the reason all the other questions about why or why not. If it could have been done, people wouldn't have been asking "why do you want to do that?" Nor would they have been suggesting "Here's another way you don't want to do it unless you have to."

    Well, anyway, thanks to all of you for trying. Bottom line, it can't be done, so I will continue getting the name of my output files the way I have so far in my Java apps. I don't find it desirable, but it's all that's availble.
    Jon Jacobs
    This message was composed entirely from recycled electrons.

Page 1 of 2 12 LastLast

Similar Threads

  1. java,jsp code to create a website statistics application
    By g.shashikala in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-15-2009, 05:00 PM
  2. Code for adding search function in an application
    By Avdhut in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-03-2009, 10:15 AM
  3. [SOLVED] Running Code in my own application
    By Doctor Cactus in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 08:38 PM
  4. organizing larger application code / modules
    By zenner in forum Advanced Java
    Replies: 5
    Last Post: 07-28-2008, 09:18 AM
  5. EventHandling code for Swing application
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-07-2007, 12:06 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
  •