Link to home
Start Free TrialLog in
Avatar of davehamer
davehamer

asked on

Execute a program with command line options from within a Java Program

Hi,

I'm currently writing a media browsing application and am trying to get the program to launch the specified media player, with a specified file (Currently developing on Mac OSX, but want to run cross-platform).

For an example command line: /Applications/VLC.app/Contents/MacOS/VLC ~/movies/my file.avi

Using

Process p = Runtime.getRuntime().exec

Or similar, I can get this to work as long as there are no spaces in either the App path or the file path.

I even wrote my own function to backslash the spaces as well ie. ~/my\ movies/my\ file.avi, but to no avail, as soon as a space occured after the beginning of the media file it didn't work.

I have searched the web and not found much relating to this at all.

I then realised that Azureus (java bit torrent client) launched files, so I downloaded the source code, and found that it used some eclipse files in order to do it.

In particular it uses org.eclipse.swt.program.Program - which automatically launches the specified file with the associated program which the operating system has for that type of file. - however I want to specify different programs for different types of files.

So... onto the actual question :) (sorry this is my first question on EE, so not sure how much information you need)

I need code to launch a program with a command line that has spaces in it, from within my java application.

Many thanks,

Dave
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Just try quoting the path

"/path with spaces/x/avi"
The command could then look something like:

String command = "startitwithsomeapp \"/path with spaces/x.avi\"";
Avatar of davehamer
davehamer

ASKER

Tried that (and just double-checked), unfortunately it still does exactly the same and only sends the first portion (before the space) to the program.
hang on, didn't see second post, will try that now
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CEHJ - had same prob using both ways :(

Cheers objects, using exec(String[] cmdarray) worked beautifully - stupidly didn't see that when I was going through the Java documentation.

Glad I got that sorted so quickly, you'll never guess how long it took me to go through that source code for Azureus :)

Thanks

Dave