Is It Possible to Make Java Find a File on Your Computer?

A

Andrew Thompson

Knute Johnson wrote:

(Lew)
At least you got my point Lew :).

(defensive) Hey! I *did* get your point, though I
may not have expressed that extremely well. I'm
confident Jeff H. did as well.

It seems much more sensible targeting the filetype
than searching for an (any) .exe that you 'reckon'
might be able to handle the file.

I thought the entire demo. with .xls and OpenOffice
should have caught the OP's attention, but they
(AFAIR) seem hell-bent on finding a file (.exe) that
does not exist on ..my system, Manish's system..

( To be honest Knute, I noticed the mention of ".xls"
in your code, took note of the method, entirely missed
the "path" part that seemed to confuse others, and
was off at the 1.6 JDocs already thinking of writing
my own little example/test. Maybe the fact I was very
drunk at the time, might explain why I wrote my own
code, rather than actually trying yours.. ;)
 
A

Andrew Thompson

[ snip ]
When I specified 'open' for that '.XLS', OpenOffice
was invoked, but it dropped the content into the
'Writer' (text editor), rather than 'Calc' (spreadsheet).

Nitpick: I'd call OO Writer a word processor rather than a text
editor, since it seems more like MS Word than, say, Notepad. No?

True. You are correct. Poor choice of phrase on my part.

And in regard to the "Skit's Law" surmise.. It was more an
expectation of the imminent application of Murphy's law - if
I was about to make comments about use of English, my
expectation was that in short course, I would make a
spectacular, public and noteworthy misuse of that very
language. "Cover my ass in advance".

But.. (now that Wikipedia page finally came up) yep!
That's it, in a nutshell! I had not realised the specialised*
form of it existed. ;-)

* And for those who want to whine about the 'z' you
expect to see in that word. Go for it, & watch me not
care..
 
B

blmblm

In other words, Desktop exploits the file association of the host OS, correct?

There used to be a lot of discussion about active data, that is, data files
that execute themselves. Helper-app associations essentially achieve that, at
least appear to, with the added flexibility of user choice about which app
actually provides the behaviors for a given data class like spreadsheets.

The fact that Desktop respects local behavior associations makes it most
potent indeed.

I read this post and thought to myself "hm, this idea of file
associations seems fairly standardized in the Windows world, but
in Unix?" So I tried the almost-self-contained example upthread
(in a post by Andrew Thompson) on a Linux box [*], and .... Wow.
Nothing very useful happens in a text-only console, which is not
a huge surprise, but under GNOME (desktop environment), opening
"empty.doc" brings up OpenOffice Writer. Pretty cool, if one likes
this sort of functionality!

[*] Fedora Core 4, if it matters.

Further investigation (skimming an entry in Sun's tutorial
about the new Desktop class and the API) indicates that file
associations are obtained via some sort of interaction with GNOME.
Experiment indicates that the "open", "browse", and "mail" actions
Just Work (to bring up appropriate programs), but that "edit"
and "print" aren't supported. Huh. I'm mildly curious about --
well, several things:

Is the lack of support for editing and printing is true on all
Linux (and Unix?) systems? If so, I wonder why.

What happens if you try this stuff under a window manager / desktop
environment other than GNOME?

Is it easy to configure things to bring up one's own choice of
"appropriate programs" rather than whatever is set up by default
(on my system, Firefox for "browse", Evolution for "mail", and for
"open" something based on the file's extension -- e.g., OO Writer
for .doc, gedit for .txt)?

Below are two pieces of code, one to check which functions are
supported and another to try the ones that are on my system (open,
browse, mail), in case anyone wants to report results on other
systems. Note that is pretty much quick-and-dirty-hack code.

==== check what's supported ====

import java.awt.*;
import java.io.*;

public class DesktopCheck {

public static void main(String[] args) throws Exception {
System.out.println("desktop supported? " +
Desktop.isDesktopSupported());
Desktop dt = Desktop.getDesktop();
for (Desktop.Action a : Desktop.Action.values()) {
System.out.println("action " + a + " supported? " +
dt.isSupported(a));

}
}
}

==== try some Desktop actions (adapted from Andrew's example) ====


import java.awt.*;
import java.io.*;
import java.net.*;

public class FileAssociation {

public static void open(File document) throws Exception {
Desktop dt = Desktop.getDesktop();
dt.open(document);
}
public static void browse(URI document) throws Exception {
Desktop dt = Desktop.getDesktop();
dt.browse(document);
}
public static void mail(URI document) throws Exception {
Desktop dt = Desktop.getDesktop();
dt.mail(document);
}


public static void main(String[] args) {
if (args.length < 3) {
System.err.println("Need parameters: " +
"filename to open, URL to browse, mail address");
System.exit(1);
}
try {
open(new File(args[0]));
} catch(Exception ex) {
ex.printStackTrace();
}
try {
browse(new URI(args[1]));
} catch(Exception ex) {
ex.printStackTrace();
}
try {
mail(new URI("mailto:" + args[2]));
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
 
B

blmblm

[ snip ]
When I specified 'open' for that '.XLS', OpenOffice
was invoked, but it dropped the content into the
'Writer' (text editor), rather than 'Calc' (spreadsheet).

Nitpick: I'd call OO Writer a word processor rather than a text
editor, since it seems more like MS Word than, say, Notepad. No?

True. You are correct. Poor choice of phrase on my part.

And in regard to the "Skit's Law" surmise.. It was more an
expectation of the imminent application of Murphy's law - if
I was about to make comments about use of English, my
expectation was that in short course, I would make a
spectacular, public and noteworthy misuse of that very
language. "Cover my ass in advance".

But.. (now that Wikipedia page finally came up) yep!
That's it, in a nutshell! I had not realised the specialised*
form of it existed. ;-)

I know about it because Skitt is a regular in alt.usage.english,
where I also lurk and sometimes post.
* And for those who want to whine about the 'z' you
expect to see in that word. Go for it, & watch me not
care..

No problem here. Now, if you were to mix UK and US spellings,
that might raise a hackle or two, but strictly one or the other --
there are better things to argue about, maybe.
 
J

Jeff Higgins

Knute said:
You missed my point completely (you weren't the only one). The
path_to_.xls is rhetorical.

No, I did not miss your point.
I named my file "path_to_.xls" as given
in your example.

Your excellent example worked for me
exactly as advertised from the very first run.

I thank you for bringing this class to my attention.

I think B.L, Massengill's example down thread is
also very nice.

All you have to specify is the path to your
 
J

Jeff Higgins

Real said:
Jeff said:
Anyway, when I run Real's example with argument
C:/Documents and Settings/Default User/path_to_.xls"
I get the exception you expressed, but
not when I run it with the DOS file path shown below.
[snip]

It's because when you have a space in the filename passed to the CMD
start command you need to specified a "title". This is a "feature" of the
start command! So something like

String[] cmdArray = {"cmd", "/c", "start", "\"\"",
"\path\with\a space\sheet.xls"};
Runtime.getRuntime().exec(cmdArray);

should do the job. The fourth item of the cmdArray is the empty title.

Oops, Thanks Real. Good case for read the famous manual.
Some links to the manual for future reference.

<http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/start.mspx>
<http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx>
<http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx>
<http://www.microsoft.com/resources/.../all/proddocs/en-us/ntcmds_shelloverview.mspx>
 
K

Knute Johnson

Andrew said:
Knute Johnson wrote:

(Lew)

(defensive) Hey! I *did* get your point, though I
may not have expressed that extremely well. I'm
confident Jeff H. did as well.

It seems much more sensible targeting the filetype
than searching for an (any) .exe that you 'reckon'
might be able to handle the file.

I thought the entire demo. with .xls and OpenOffice
should have caught the OP's attention, but they
(AFAIR) seem hell-bent on finding a file (.exe) that
does not exist on ..my system, Manish's system..

( To be honest Knute, I noticed the mention of ".xls"
in your code, took note of the method, entirely missed
the "path" part that seemed to confuse others, and
was off at the 1.6 JDocs already thinking of writing
my own little example/test. Maybe the fact I was very
drunk at the time, might explain why I wrote my own
code, rather than actually trying yours.. ;)

Hey I stand corrected. You and Jeff got what I was saying even when I
didn't know :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top