1

I am trying to open file in external editor from java, but when i run my source code, nothing happens. I am using JRE 1.6 and my opration system is Windows 7. Here is my source code:

Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
  desktop = Desktop.getDesktop();
}

 desktop.edit(new File("D:\\Document.rtf"));
8
  • Why don't you debug by putting some System.out.println() statements in the if block and also are there any exceptions thrown? This worked fine for me. Commented May 27, 2012 at 14:35
  • What happens when you try to open an rtf file on your machine without Java? Commented May 27, 2012 at 14:35
  • These 5 lines aren't enclosed inside try {...} catch (Exception e) {}? If they are, then you have your answer: don't ignore exceptions. Commented May 27, 2012 at 15:46
  • when i am trying to open that file in my machine, it is working correctly. Commented May 27, 2012 at 16:38
  • sorry for confusion, in my real source code i am catching exceptions. When i run my code, no exception is thrown and in the debugger i see, that command desktop = Desktop.getDesktop(); is really executed. Commented May 27, 2012 at 16:40

2 Answers 2

1

The following should also work:

Runtime.getRuntime().exec( "cmd /C D:\\Document.rtf" );

or

    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 " + file);
    } else {
        //run.exec("cmd.exe /c start " + file); //win NT, win2000
        run.exec("rundll32 url.dll, FileProtocolHandler " + path);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think the OP should be given an alternate solution,rather be told what his mistakes are in the current code.
first solution is working nice for me. Thank you very much for that, you solved my problem. But i am still curious, why my original source code was not working ... hmm :)
0
  • did you read API

public void edit(File file) throws IOException

Launches the associated editor application and opens a file for editing.

Parameters: file - the file to be opened for editing Throws: NullPointerException - if the specified file is null IllegalArgumentException - if the specified file doesn't exist UnsupportedOperationException - if the current platform does not support the Desktop.Action.EDIT action IOException - if the specified file has no associated editor, or the associated application fails to be launched SecurityException - if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file, or SecurityManager.checkWrite(java.lang.String) method denies write access to the file, or it denies the AWTPermission("showWindowWithoutWarningBanner") permission, or the calling thread is not allowed to create a subprocess See Also: AWTPermission

1 Comment

hi, I tested both of these exception as you suggested, but none of them is thrown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.