7 Replies Latest reply: Sep 15, 2010 2:52 PM by Darryl Burke RSS

    Launch external files on Linux

    843810
      I want to launch external files from Java, in a cross platform way.

      ie say I have a file fred.jpg, I want to launch that on any platform to open in the editor defined on that computer.

      The book 'Swing Hacks' (Marinaccci and Adamson), shows how to do this for Windows and Mac. My version of their code is

           
      static public void launch(URL url){
                try{
                     String cmd;
                     
                     if (OS.isMac())
                          cmd = "open "+url;
                     else     // TODO linux?
                          cmd = "cmd.exe /c start "+url;     
                     
                     Runtime.getRuntime().exec(cmd);
                }
                catch(Exception e){new Fail(e);}          
           }
      However, I don't know what to put in for linux?

      Searching the web I came across http://weblogs.java.net/blog/ixmal/archive/2006/05/javaawtdesktop.html,
      which suggests that
      Desktop.getDesktop().open(new File(filename));
      is more platform independant. However for me it seems to work only on Windows, failing silently on Mac and Linux?

      Can someone with a good knowledge of Linux help please.
        • 1. Re: Launch external files on Linux
          843810
          Will Duke stars get anyone to answer this?
          • 2. Re: Launch external files on Linux
            793415
            >
            Will Duke stars get anyone to answer this?>
            I cannot say. What I can tell you is that at least it got me to read it.

            And now I look over your original post..
            >
            ..However for me it seems to work only on Windows, failing silently on Mac and Linux?>
            'Seems'? The docs. for Desktop.open(File) suggest it throws a whole range of exceptions if it cannot perform the task. Are you sure the code is not 'swallowing exceptions'?

            Can you produce an SSCCE that fails as you claim? If so, this really sounds like a bug.

            And as an aside, Desktop was introduced in 1.6, but AFAIR, Apple is still stuck on 1.5. How did you get it to 'fail silently' on a Mac.?

            Edit 1:

            But if you are intent on doing it the way that you originally asked about, you might be able to pick up some tips from the [http://browserlaunch2.sourceforge.net/|BrowserLauncher2] code.

            Edited by: AndrewThompson64 on May 22, 2008 4:48 PM
            • 3. Re: Launch external files on Linux
              843810
              Andrew, thank you, I think you have given me all the clues I need to solve my problem.

              To elaborate,

              I think 'fail silently' was unfair, it was me gobbling the exceptions silently.
              I hadn't noticed that Desktop was only available in java v6 and later
              The Linux machine I used had a Java version of 5.0 update 10, so did not have the new Desktop functions.

              So, I need to code,

              if Java version 5
              if Mac, run old exec("open ...")
              if Linus, give warning message, "update your java"
              if Windows, run old exec("cmd.exe ...")
              if Java version 6
              Desktop.getDesktop.open( )

              I will code it, test it, and then award you the Dukes.
              • 4. Re: Launch external files on Linux
                843810
                well i dont konow it its helps but:
                I also tried to launch external applications on my Mac, but it also diddnt work.
                I already gave up but trying these 2 Code of lines made it for me:
                String[] cmd = {"open", "/Applications/Front Row.app/"};
                Runtime.getRuntime().exec(cmd);
                So it seems as you need to array your command line
                simple open /Applications/Front Row doesnt Work.
                • 5. Re: Launch external files on Linux
                  843810
                  Why don't you use
                  if (Desktop.isDesktopSupported()) {
                       try {
                            Desktop.getDesktop().open(new File(fileName));
                       } catch (IOException e) {
                            e.printStackTrace();
                       }
                  }
                  • 6. Re: Launch external files on Linux
                    794044
                    I don't use that because the default program for JavaScript is then launched, while I want Google Chrome to be launched.
                    EDIT: I can ask my users to make Google Chrome the default application but I prefer Java doing the right thing.

                    Edited by: The_Pointer on Sep 15, 2010 9:20 AM
                    • 7. Re: Launch external files on Linux
                      Darryl Burke
                      The_Pointer, don't post in threads that are long dead and don't hijack another poster's thread. You already have your own thread for your question, which is not the same as the one discussed here.

                      I'm locking this thread now.

                      db