mehdi
mehdi

Reputation: 686

how do i open and run(execute) a file at java?

i am trying to develop a j2se program that copy a .csv for special task. Now i want to run(execute) that copied .csv file.

if copied paht is "C:\program files\reports\test.csv" or C:\Documents and Settings\User\My Documents\test.csv" it dose not work for this code:

run.exec("cmd start /c C:/Documents and setting\user\My Documents\test.csv");

and for this code:

if path gets form a JTextField or JFileChooser, how is works?

note taht in during of running of this program users may set their special path.

thank u for your answer.

Upvotes: 0

Views: 1831

Answers (2)

Michael Borgwardt
Michael Borgwardt

Reputation: 346357

You should use the Desktop class introduced in Java 6 if possible:

Desktop.getDesktop().open(new File(filename));

Less problems with separating commands, and it's platform-independant.

Upvotes: 2

Bozho
Bozho

Reputation: 597204

Try adding quotes around the path, and either use forward slashes, or use double backward slashes:

Runtime.getRuntime().exec(
    "cmd start /c \"C:/Documents and setting/user/My Documents/test.csv\"");

Update: Use Michael's solution for Java 6.

Upvotes: 1

Related Questions