1 2 3 Previous Next 31 Replies Latest reply on May 2, 2008 4:07 PM by 807601

    Opening a Word File

    807601
      Hey

      I got a question for a program that im programming im trying to program a button
      that opens a word File the moment u click on the button. Anychance any of u know how to realize this ?
      By opening the word file i actually mean that java just locates the file and opens it with the standaard program chosen for the .doc extension

      Please let me know.

      Steven
        • 1. Re: Opening a Word File
          807601
          Here I have build in a little check if its a Mac OS or Windows.
          The file you open is on the C:\ device.
          Runtime run = Runtime.getRuntime();
          String lcOSName = System.getProperty("os.name").toLowerCase();
          boolean MAC_OS_X = lcOSName.startsWith("mac os x");
          if(MAC_OS_X)
          {
            run.exec("open C:\File.doc");
          } 
          else 
          {
           run.exec("cmd.exe /c start C:\File.doc");
          }
          Edited by: Nimzor on May 2, 2008 6:34 AM
          • 2. Re: Opening a Word File
            807601
            Cross posted from http://forum.java.sun.com/thread.jspa?threadID=5292160&tstart=0 .
            • 3. Re: Opening a Word File
              807601
              Nimzor wrote:
              Here I have build in a little check if its a Mac OS or Windows.
              The file you open is on the C:\ device.
              Runtime run = Runtime.getRuntime();
              String lcOSName = System.getProperty("os.name").toLowerCase();
              boolean MAC_OS_X = lcOSName.startsWith("mac os x");
              if(MAC_OS_X)
              {
              run.exec("open C:\File.doc");
              } 
              else 
              {
              run.exec("cmd.exe /c start C:\File.doc");
              }
              Edited by: Nimzor on May 2, 2008 6:34 AM
              which will fail if the OS is Linux or AIX or Solaris etce etc etc.
              • 4. Re: Opening a Word File
                807601
                i get several errors when i use ur code.

                init:
                deps-jar:
                Compiling 1 source file to C:\Documents and Settings\Administrator\Bureaublad\I-1\Last moment\ProjectSportivo\build\classes
                C:\Documents and Settings\Administrator\Bureaublad\I-1\Last moment\ProjectSportivo\src\projectsportivo\Overzichten.java:214: illegal escape character
                run.exec("open C:\File.doc");
                C:\Documents and Settings\Administrator\Bureaublad\I-1\Last moment\ProjectSportivo\src\projectsportivo\Overzichten.java:218: illegal escape character
                run.exec("cmd.exe /c start C:\File.doc");
                2 errors
                BUILD FAILED (total time: 0 seconds)
                • 5. Re: Opening a Word File
                  807601
                  Have you got a file named ?File.doc? on your C:\ ?

                  Also import this :
                  import java.io.File; 
                  import java.io.IOException;
                  • 6. Re: Opening a Word File
                    807601
                    sabre150 wrote:
                    Nimzor wrote:
                    Here I have build in a little check if its a Mac OS or Windows.
                    The file you open is on the C:\ device.
                    Runtime run = Runtime.getRuntime();
                    String lcOSName = System.getProperty("os.name").toLowerCase();
                    boolean MAC_OS_X = lcOSName.startsWith("mac os x");
                    if(MAC_OS_X)
                    {
                    run.exec("open C:\File.doc");
                    } 
                    else 
                    {
                    run.exec("cmd.exe /c start C:\File.doc");
                    }
                    Edited by: Nimzor on May 2, 2008 6:34 AM
                    which will fail if the OS is Linux or AIX or Solaris etce etc etc.
                    Well wouldn?t that be something he should figure out ?
                    It?s his environment, his project, his assignment.
                    I certainly don?t have to write everything for him, do I ?
                    • 7. Re: Opening a Word File
                      807601
                      Yeah i got a file with that name on my C disk
                      and im working on windows btw i saw sabre said that the script would fail anyways
                      even if it did compile ?
                      • 8. Re: Opening a Word File
                        807601
                        The 'escape problem' is solved using
                         "c:\\File.doc". 
                        The second \ being needed because \ is used as an escape character in Java. You still need to read the reference I gave.

                        Edited by: sabre150 on May 2, 2008 2:47 PM
                        • 9. Re: Opening a Word File
                          807601
                          Nevermind on the OS part.
                          That one is fine :p
                          its only gonna run on windows so thats no problem.
                          But it says


                          init:
                          deps-jar:
                          Compiling 1 source file to C:\Documents and Settings\Administrator\Bureaublad\I-1\Last moment\ProjectSportivo\build\classes
                          C:\Documents and Settings\Administrator\Bureaublad\I-1\Last moment\ProjectSportivo\src\projectsportivo\Overzichten.java:215: illegal escape character
                          run.exec("open c:\Aanmaning.doc");
                          C:\Documents and Settings\Administrator\Bureaublad\I-1\Last moment\ProjectSportivo\src\projectsportivo\Overzichten.java:219: illegal escape character
                          run.exec("cmd.exe /c start c:\Aanmaning.doc");
                          2 errors
                          BUILD FAILED (total time: 0 seconds)

                          I changed the code filename
                              private void MbriefMouseClicked(java.awt.event.MouseEvent evt) {
                          Runtime run = Runtime.getRuntime();
                          String lcOSName = System.getProperty("os.name").toLowerCase();
                          boolean MAC_OS_X = lcOSName.startsWith("mac os x");
                          if(MAC_OS_X)
                          {
                            run.exec("open c:\Aanmaning.doc");
                          } 
                          else 
                          {
                           run.exec("cmd.exe /c start c:\Aanmaning.doc");
                          }
                          
                              }
                          • 10. Re: Opening a Word File
                            darrylburke
                            Reposted from
                            [http://forum.java.sun.com/thread.jspa?threadID=5292160]

                            db
                            • 11. Re: Opening a Word File
                              807601
                              You still have not used \\ !
                              • 12. Re: Opening a Word File
                                807601
                                Look what sabre550 posted to you.

                                Change c:\Aanmaning.doc to
                                c:\\Aanmaning.doc
                                Edited by: Nimzor on May 2, 2008 6:50 AM
                                • 13. Re: Opening a Word File
                                  807601
                                  init:
                                  deps-jar:
                                  Compiling 1 source file to C:\Documents and Settings\Administrator\Bureaublad\I-1\Last moment\ProjectSportivo\build\classes
                                  C:\Documents and Settings\Administrator\Bureaublad\I-1\Last moment\ProjectSportivo\src\projectsportivo\Overzichten.java:215: unreported exception java.io.IOException; must be caught or declared to be thrown
                                  run.exec("open c:\\Aanmaning.doc");
                                  C:\Documents and Settings\Administrator\Bureaublad\I-1\Last moment\ProjectSportivo\src\projectsportivo\Overzichten.java:219: unreported exception java.io.IOException; must be caught or declared to be thrown
                                  run.exec("cmd.exe //c start c:\\Aanmaning.doc");
                                  2 errors
                                  BUILD FAILED (total time: 0 seconds)


                                  Got that error message now i edited the double /
                                  like u said but its mention it needs 2 be caught or declared to be thrown
                                  gonna check ur reference ... again :P
                                  • 14. Re: Opening a Word File
                                    807601
                                    @sabre:

                                    The solution you gave in the cross posted thread isn't really suitable for linux/solaris/mac either.

                                    Or is rundll32 a process on linux/solaris/mac aswell ? If so I apologize.
                                    1 2 3 Previous Next