14 Replies Latest reply: Apr 12, 2008 9:56 PM by 807591 RSS

    How to open files using default association

    807591
      Hello,

      I'm trying to programmatically open files with their OS default associations. So for windows platform I have:

      Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath());

      This does not work when file names contain unicode characters like "�s����.jpg". Does anyone have a solution for this? What is the proper and recommended way to open files?

      Thanks,
      Marcin
        • 1. Re: How to open files using default association
          800351
          Try java.awt.Desktop class. I can gurantee nothing, though...
          • 2. Re: How to open files using default association
            807591
            I would appreciate a solution that works with JDK 1.4, anyone else has a suggestion?
            Marcin
            • 3. Re: How to open files using default association
              DrClap
              First of all, every character is a Unicode character. Don't follow the herd blindly and assume that Unicode means "any character that causes me a problem".

              Second, does that rundll32 command work with those file names when you type it at the command line?
              • 4. Re: How to open files using default association
                807591
                The rundll32 works from the command line with double byte japanease characters in the filename, but not from exec() function.
                • 5. Re: How to open files using default association
                  800351
                  This does not work
                  Is the thing which does not work the File instantiation, the getAbsolutePath() call or the Runtime.exec() call? Or someting other? Have you isolated the exact error point?
                  when file names contain unicode
                  Do you mean your code works as is when the file name consists of simple ASCIIs?
                  exec("rundll32
                  Doesn't it require "command", "cmd", "start" or something similar and an option like "/C" or whatever? We usually use exec(new String[] {...}) for them.
                  japanease characters
                  Are the default locale/LANG of your system that the JVM assumes and actual encoding on the system/file_system compatible? For instance, if the one is UTF-8 and other is EUC, things won't work.
                  • 6. Re: How to open files using default association
                    807591
                    Hi,

                    Do you have any specfic folder that you want to open.

                    The best solution i feel is

                    String path =System.getProperty("user.home");

                    path+"/tes.txt";

                    this way u can open tes.txt in any os in user home directory

                    Edited by: KodandaRamaiah on Apr 12, 2008 10:56 PM
                    • 7. Re: How to open files using default association
                      807591
                      My code actually creates and writes the bytes to the file from other stream. The file is created and written correctly but when I pass its instance object for opening which uses Runtime.exec() then this fails. The exec() return code is 0 (zero). When I double click on the file in windows, it opens and is not corrupted so I know my file creation function worked. My default locale is US and language English. I realize that this file comes from a Japanease system and I'm trying to write code that would download and open a file regardless from what locale it comes from.
                      • 8. Re: How to open files using default association
                        jschellSomeoneStoleMyAlias
                        mkurzawa wrote:
                        My code actually creates and writes the bytes to the file from other stream. The file is created and written correctly but when I pass its instance object for opening which uses Runtime.exec() then this fails. The exec() return code is 0 (zero).
                        No.

                        Those two statements are contradictory.

                        If runtime exec returned zero then it means that did in fact work in java.

                        So if it didn't "work" then it means that it didn't work solely and completely in terms of the OS and the specific commands and syntax that you are using in terms of that.

                        And you specifically need to explain what "it didn't work" means.

                        ... double click on the file in windows, it opens and.... I realize that this file comes from a Japanease system...
                        You have a file on an english version of windows which has a "japanese" name?
                        ....and open a file regardless from what locale it comes from.
                        And what do you mean when you say "open" it? I can certainly open any file on the system using a binary open call.

                        That is far different than displaying text or text and other information in any intelligent format. And I suspect creating something to do it for absolutely any file is not going to be possible.
                        • 9. Re: How to open files using default association
                          807591
                          When I say "it didn't work" I mean that the file did not open with default system association. The DLL returns zero (0) but file does not open. When I run the same command from a command prompt it works well, meaning file opens with default association. This leads me to believe that DLL runs but the filename gets misinterpreted, and perhaps incorrectly converted (by Java and not native call) instead of being left in its original double byte form.

                          The file is a binary file, for example a JPG file that has a Japanese name or any other name that uses double byte characters.
                          Yes, I do have a file on an english version of windows which has a Japanese name.

                          To open a file I use the command:

                          Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath());

                          This works great for US-ASCII filenames, but not for double byte filenames. This command always works for all JPG files from command prompt, but not for double byte filenames from Java code. It does work in Java code for regular English filenames. By "open" I mean use the default association to open it. I'm after the same "open" as would happen when I double click on it in Windows Explorer, and not opening a stream for reading or writing inside Java code.
                          • 10. Re: How to open files using default association
                            jschellSomeoneStoleMyAlias
                            mkurzawa wrote:
                            When I say "it didn't work" I mean that the file did not open with default system association. The DLL returns zero (0) but file does not open. When I run the same command from a command prompt it works well, meaning file opens with default association. This leads me to believe that DLL runs but the filename gets misinterpreted, and perhaps incorrectly converted (by Java and not native call) instead of being left in its original double byte form.

                            The file is a binary file, for example a JPG file that has a Japanese name or any other name that uses double byte characters.
                            Yes, I do have a file on an english version of windows which has a Japanese name.

                            To open a file I use the command:

                            Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath());
                            How do you type that file name into a console window on an english system?

                            Perhaps using alt key combinations?

                            So try doing that into a notepad and save it as text document.
                            Then using the above resolved 'cmd' write it to a file instead of using exec().
                            Also use 'dir' from the command line and redirect to text file.

                            The compare the byte (not character) contents of all three and see if there is any difference.
                            • 11. Re: How to open files using default association
                              807591
                              I do not type the name of the file, I use copy and paste.

                              Saving the command into notepad would require me to choose an encoding which changes the byte content.
                              The 'dir' command gives ANSI output on my system so all double byte characters are lost in the conversion process.

                              I need a proper solution, not a hack. The solution has to work for all locales because this piece of software will be installed on computers in most countries.
                              • 12. Re: How to open files using default association
                                jschellSomeoneStoleMyAlias
                                mkurzawa wrote:
                                I do not type the name of the file, I use copy and paste.
                                Not sure there is much of a difference in terms of the command line, nor in what I suggested. Mainly that you need to get the bytes into a file so you could actually figure out if they are different. And how.
                                Saving the command into notepad would require me to choose an encoding which changes the byte content.
                                The 'dir' command gives ANSI output on my system so all double byte characters are lost in the conversion process.

                                I need a proper solution, not a hack. The solution has to work for all locales because this piece of software will be installed on computers in most countries.
                                I suspect that the command you are using, regardless of a the file name, is a hack. Certainly won't work on anything but windows and only some versions of that.

                                Other than that I would suppose that there are not many english speakers (and users of english windows) that routinely use japanese file names. I suppose the reverse is more likely but as far as I can recall there is more support for that on the japanese version as well.
                                • 13. Re: How to open files using default association
                                  807591
                                  This is just a pure shot in the dark, but it's worth trying:

                                  Don't use the Runtime#exec(String) call. Instead, use one of the overloads that takes a String[] array for its command:
                                  Runtime#exec(String[])
                                  It could possibly be that, when using the "String command" version of the method you are using, the Unicode characters are causing the command String to be tokenized incorrectly. If this is the case, using the String[] version will allow you to be explicit about the program and its arguments.

                                  Again, this is just an idea, and could be wrong/irrelevant/whatever else.

                                  If you have an SWT dependency already, you could use the org.eclipse.swt.program.Program class. :)
                                  • 14. Re: How to open files using default association
                                    807591
                                    Thanks for the suggestion, but the bug still persists...