Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: Determining the location of the file that initiates an application

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Determining the location of the file that initiates an application

    Hi,

    My question is, suppose I have an xml file say "myXml.xml" that can be stored in any directory on my PC and I set its default program (the one that will run when I double click myXml to "Myapplication.exe"
    How do I determine the location of this file "myXml" when Myapplication starts, using java?
    (Myapplication.exe is being programmed using java)

    The reason I ask is that myXml would theoretically store application setting for Myapplication.exe and I would want to load them directly on start up if myApplication.exe is initiated by double click of an xml file

    I understand how to use file objects and all that, I just dont know how to get either the location of this file "myXml" or its contents without hard coding its location within the application itself, cos as I said this file could be located virtually anywhere on the pc

    Any help would be much appreciated!!!

    Best regards
    Michael


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Determining the location of the file that initiates an application

    I'm a little confused by your statement that the .exe file is being programmed in java, since it's unusual for a Java program to be compiled into an .exe file, though not impossible. I'll let that go for now.

    I don't know the answer, but I imagine it has something to do with the launch mechanism of the host operating system. Since double-clicking a file (or in some cases single-clicking) opens that file or launches it in its associated application, the filename and full path name of that file must be passed to the associated application. I assume that the filename is passed to the associated application by the OS using a mechanism similar to the command line arguments in a console-run Java program. You may need an OS-specific .bat file or script that accepts the selected .xml file name and passes it to the Java program as a parameter.

    Keep searching and let us know what you find out.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Hi Greg, to clarify I am making a normal runnable jar file and then I am going to convert it into a .exe for my windows version just to make it neat on the platform.
    Onto the problem at hand, my instinct is similar to what you have put forward here, but as of yet I have come up empty in my search for the answer

    I would still appreciate anyone weighing in with some wise words, cos surely this is a problem that all applications that can load files needs to overcome at some point...

    All help is greatly appreciated!!
    Best regards
    Michael

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    I think in Windows, when the OS starts a program to process a file that was doubled clicked, the commandline associated with that type file from the registry is filled in and executed. One of the parameters on a normal commandline is the path to the file that was double clicked.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Ok, so far I have found a good half solution, java has System.getProperty("user.dir") which gives me the working directory of whatever initiated running my program. So the only thing I need is to get the actual file name that initiates my program, any ideas?

    I also went and listed all the available system properties with:
    Properties p = System.getProperties();
    p.list(System.out);

    But the actual filename that executes the application was not among them

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    get the actual file name that initiates my program,
    Are you asking what file is being passed on the commandline to your program when the OS starts your program because the file was double clicked?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Yes, that is what I am asking, if we are both understanding each other correctly.
    So let me explain clearly, say my application "myApp.exe" is located at C:\Users\Michael\Desktop and I have set it to be the default program for xml files.
    Now say that I have an xml file "myXml.xml" located at "C:\Users\Michael\Desktop\Development", therefore the full path of this file would be "C:\Users\Michael\Desktop\Development\myXml.xm l"

    Now so far what I have found is that System.getProperty("user.dir") returns the current working directory and so if I double clicked myXml.xml this would return "C:\Users\Michael\Desktop\Development" as the current working directory.
    The problem is I have no way so far of finding out the actual file name, in this case "myXml.xml"

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    The args from the command line are passed to the main() method in a String array.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Maybe I'm not understanding you, but the file-name doesn't get passed to the command line,
    can you perhaps give me a small example of what you mean?

    Thanks!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    but the file-name doesn't get passed to the command line,
    Yes that is possible. It depends on the OS and how its file association table is setup. Was the String array passed to main() empty?

    On my Windows7 system, I have the commandlines in the file assocation table (in the registry) set to pass the path to the file that was double-clicked on the commandline. It is not necessary that the path to the file be on the commandline. I don't know why anyone would configure it that why, but I think it is possible.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Hi Norm,
    yes the string passed to main was empty, so im not sure how to get at the information, i.e. the name of the file?
    Other than editing how the file association table, is there another way cos that seems like quite an impractical solution when distributing this application

    Thanks for all your help so far, I'm quite out of my depth here so I appreciate it!

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    What is the commandline that is now in the file association table in the registry that starts the execution of your program?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    I'm not sure what you are asking, so I am just gonna Google a bit so I can give you an intelligent answer, ill reply shortly

  14. #14
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Ok, so iv looked around a bit through my registry, do you mean this entry, Iv attached a png

    1.jpg


    I also looked through the FileExt list and there was no command line that I could see, I added a custom file association called .cmi and that is what is displayed here

    2.jpg

    Thanks!!

    Let me know if you cant see the images cos they have come out really small on the site for some reason

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    Use the regedit program's export feature to create a file from a registry entry and then copy and paste that entry here. Here is an exported entry from my registry with a command line with a %1 where the OS substitutes the path to the file:
    Windows Registry Editor Version 5.00
     
    [HKEY_CLASSES_ROOT\NormsEDPFile]
    "EditFlags"=dword:00000000
    "BrowserFlags"=dword:00000008
    @="Norms Enc-Dec File"
     
    [HKEY_CLASSES_ROOT\NormsEDPFile\shell]
    @="Open"
     
    [HKEY_CLASSES_ROOT\NormsEDPFile\shell\Open]
     
    [HKEY_CLASSES_ROOT\NormsEDPFile\shell\Open\command]
    @="java.exe -jar D:\\JavaDevelopment\\runtime\\NormsEncDec.jar \"%1\""
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Hi Norm, Ok I hope this is what you meant!
    I took the file association that I have made and am using for my application and I also took the registry entry from the same location as you had yours




     
    Windows Registry Editor Version 5.00
     
    [HKEY_CLASSES_ROOT\.cmi]
    @="cmi_auto_file"
     
    [HKEY_CLASSES_ROOT\.cmi\PersistentHandler]
    "OriginalPersistentHandler"="{00000000-0000-0000-0000-000000000000}"

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    What is in the cmi_auto_file entry. That would be where the commandline for the .cmi file would be.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Ok cool, this is whats currently in that file directory

    Windows Registry Editor Version 5.00
     
    [HKEY_CLASSES_ROOT\cmi_auto_file]
    @=""
     
    [HKEY_CLASSES_ROOT\cmi_auto_file\shell]
     
    [HKEY_CLASSES_ROOT\cmi_auto_file\shell\open]
     
    [HKEY_CLASSES_ROOT\cmi_auto_file\shell\open\command]
    @="\"C:\\Users\\Michael\\Desktop\\CMIApplication.exe\" \"%1\""
    Should I fix the quotes to be in the same format as yours are in the example you gave me?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    I thought you were working with a java program. The commandline in the post has a .exe in it. I have no idea what that .exe program will do with any parameters passed on a commandline.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    I am working with a java program, but its a java program that I have bundled into a exe using Launch4j, I can change it to point to my jar file though, what do you suggest?

    Thanks for your patience, I'm learning a lot!!

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    I have no idea what that .exe file does. I know that my jar files work.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Hey Norm, thanks for your help so far,
    my laptops screen died and is being repaired which is why I have been unable to reply till now

    Can I ask what does \"%1\"" actually do in the registry command line? I know you mentioned it but I didnt really understand
    Thanks so much!!

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Determining the location of the file that initiates an application

    The OS replaces the %1 with the value of the path to the file that was double clicked.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Determining the location of the file that initiates an application

    Ok cool, then as soon as I get my laptop back Ill do some experimenting, and ill report back with anything I discover!!
    Thanks Norm

Similar Threads

  1. Hardcode a file location into code, removing user input
    By PB94941 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2013, 06:09 AM
  2. SAX parser using wrong file location
    By aueddonline in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 8th, 2012, 10:56 AM
  3. [SOLVED] [help] the application file (.jad) for application does not appear to be the ....
    By ordinarypeople in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: April 4th, 2010, 03:50 AM
  4. Image location on a Runnable JAR file?
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 07:59 AM
  5. [SOLVED] File Reading from the specified location
    By rajesh.mv in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 13th, 2009, 12:56 AM