25

OK. Simple question. Maybe not so simple answer, though:

I have a file I downloaded in Java, and I know that it's a text file. Is there any way that I can use Java to open that text file in whatever the default text editor is? It has to work for all OS's, otherwise I would just make it open with Notepad.

:\ I guess that if there's no way to do this I could use JOptionPane and show the contents of the text file...

Petros Koutsolampros
  • 2,790
  • 1
  • 14
  • 20

3 Answers3

38

You can do that with:

java.awt.Desktop.getDesktop().edit(file);

This links to the tutorial article on java.awt.Desktop:

Java™ Standard Edition version 6 narrows the gap between performance and integration of native applications and Java applications. Along with the new system tray functionality, splash screen support, and enhanced printing for JTables , Java SE version 6 provides the Desktop API (java.awt.Desktop) API, which allows Java applications to interact with default applications associated with specific file types on the host platform.

It is cross-platform, but may not be supported everywhere. There is a method you can call to check whether the Desktop API is available, called isDesktopSupported (see the link for more explanation). I was using this API the other day to open PDFs in a Swing client.

Unfortunately there is a known bug affecting some Windows platforms (XP and 2003) that will crash the JVM. Write once, debug everywhere, as usual. Anyway, for Windows there is a nice workaround which still uses the user's preferred application:

if (System.getProperty("os.name").toLowerCase().contains("windows")) {
  String cmd = "rundll32 url.dll,FileProtocolHandler " + file.getCanonicalPath();
  Runtime.getRuntime().exec(cmd);
} 
else {
  Desktop.getDesktop().edit(file);
}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • OK Cool-- that's cross-platform and handles everything? :) –  Jun 08 '11 at 01:09
  • 2
    Ah, wasn't aware of Desktop -- my Java pretty much stops with 5. – Hot Licks Jun 08 '11 at 01:17
  • 3
    Don't forget there is also `Desktop.open(File)` (sometimes opens read-only) & `Desktop.print(File)` for slightly different actions. – Andrew Thompson Jun 08 '11 at 04:30
  • That bug report states.. *"Only with Windows 2003 , **works fine with winXP.**"* (emphasis mine) If Win. 2003 is all it crashes on, I won't be losing sleep over it. Specify XP as the minimum and lose a few clients - no stress. – Andrew Thompson Jul 29 '11 at 11:45
  • @Andrew: Yeah, it says that. But my boss' boss was running my program, *on XP*, and it crashed for him. So I was stressed. – Nathan Hughes Jul 29 '11 at 13:05
  • Only problem here: When you have http://someserver/somedir/myReport.doc then http takes priority over .doc and instead of Word/OpenOffice the browser opens. This is a Windows limitation. – stwissel Sep 02 '11 at 03:09
  • @stwissel: would it help to map a drive letter on the client to the webdav server, then use the drive letter instead of http? – Nathan Hughes Sep 02 '11 at 13:35
  • 1
    does not work on Ubuntu 14.04 - but Desktop.open(File) does, thanks to @AndrewThompson – Murmel Oct 12 '16 at 12:55
  • Desktop.getDesktop().edit( ) / open( ) work only if a default editor is already associated with that extension. Else it crashes (i ran it on win 10) saying that "No application is associated with the specified file..." Isn't there a way to just open the default text editor (whatever) of the current OS? – Dia Sheikh Jan 17 '22 at 14:25
6
Desktop.getDesktop().edit(File f);
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
Stas Jaro
  • 4,747
  • 5
  • 31
  • 53
0

Certainly you could configure in the text editor and use Runtime.exec to start it. But I can't think of any way to determine the default editor, especially in a system-independent fashion.

Maybe your best option is to identify which of the several most popular platforms you're on and then find a way to start the default editor on that platform. Eg, on Window you'll get the default editor if you do "start filename.txt", and I'm pretty sure there's a Linux equivalent.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151