using a shell open from within java program

J

jcs79

I want to write code in Java that will open a selected file using the
shell's default application for opening a file of that type. For
insatnce, I select an mp3 file on my disk and the java program tells
the OS to open that file using its default application and plays it.
Can anyone help me?
 
F

fabienne hadek

yep

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(new String[] { "/bin/sh", "-c",
"./processfile.sh " + parameter}); // or whatever you want to do....
int exitVal = proc.waitFor();
out.println("Process exitValue: " + exitVal);

fabienne
 
J

jcs79

fabienne hadek said:
yep

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(new String[] { "/bin/sh", "-c",
"./processfile.sh " + parameter}); // or whatever you want to do....
int exitVal = proc.waitFor();
out.println("Process exitValue: " + exitVal);

fabienne
This is a start, but I dont want to run a specific program, I want to
run whatever a default program is given a specific file type, e.g. if
I use a filechooser to select an mp3, java sends the command to
windows to run whatever application the user has set up to play mp3
files.
 
M

Matthew Zimmer

jcs79 said:
fabienne hadek said:
yep

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(new String[] { "/bin/sh", "-c",
"./processfile.sh " + parameter}); // or whatever you want to do....
int exitVal = proc.waitFor();
out.println("Process exitValue: " + exitVal);

fabienne

This is a start, but I dont want to run a specific program, I want to
run whatever a default program is given a specific file type, e.g. if
I use a filechooser to select an mp3, java sends the command to
windows to run whatever application the user has set up to play mp3
files.

Okay, basically to do this you have to look into the windows registry to
figure out what the default application is. There are some open source
code out there that allows you to access the registry, but I don't
remember what they are right now. Just google it and you should find
something. Anyway, once you get the code to let you access the registry
you follow these basic steps:

1. Get the extension of the file.
2. Look in the registry for the default program for that extension.
The basic location in WinXP for that is "HKEY_CLASSES_ROOT\extension".
3. Take the resulting program string (PROGRAM_STRING) and then look for
the open command for that program. The basic location for that is
"HKEY_CLASSES_ROOT\PROGRAM_STRING\shell\open\command".
4. Use that as the first argument in the exec command as listed above.

Yes, this may seem like a pain, but it does work. I know this for a
fact as I've used it in the past. Hope that helps.

Matthew
 
M

Michael Borgwardt

Matthew said:
Okay, basically to do this you have to look into the windows registry to
figure out what the default application is.

Or you can let windows do that for you:

Runtime.getRuntime().exec("cmd /C\"start "+filename);
 
M

Matthew Zimmer

Michael said:
Or you can let windows do that for you:

Runtime.getRuntime().exec("cmd /C\"start "+filename);

That's mostly true. I ran into some errors when I tried using that in
various instances. Also, it will not work at all (IIRC) in Windows95
and probably not 98 either. However, it is true that it should work
most of the time. One additional note about this (well, more of a
question since I don't remember what it does exactly). Doesn't using
that command pop up a dos window in addition to the program?
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top