[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cannot open a file using Runtime.getRuntime().exec("cmd /c start filename"); in java

Posted on 2007-08-06
10
Medium Priority
?
1,250 Views
Last Modified: 2012-05-05
I am trying to open a file using
Runtime.getRuntime().exec("cmd /c start fileName");

It works file when the fileName is not having any space.

but when i tried with "C:\Documents and Settings\sandeep\san.xls"
it is not opening

Thanks
0
Comment
Question by:sangameshrh
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 19637330
Try

Runtime.getRuntime().exec("cmd /c start  \" + fileName + " \"");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19637332
Sorry

Runtime.getRuntime().exec("cmd /c start  \"" + fileName + " \"");
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 19637343
That's because the command interpreter (cmd.exe) has no idea where one parameter starts and ends.

You need to escape them so try this:

"cmd.exe /C start \"C:\\Documents and Settings\\sandeep\\san.xls""

ellandrd
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 19637364
Use this:

Runtime.getRuntime().exec(new String[]{"cmd", "/c", "start", fileName});
0
 

Author Comment

by:sangameshrh
ID: 19637680
None is working.
I have tried
Runtime.getRuntime().exec("cmd.exe /C start \"C:\\Documents and Settings\\sandeep\\Desktop\\test.txt\"");

But it is opening a command window(cmd.exe)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 19637724
Try

Runtime.getRuntime().exec(String.format("%s \"%s\"",  "C:/WINDOWS/notepad.exe", fileName);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19637727
(Missed closing bracket on that last)
0
 
LVL 16

Assisted Solution

by:ellandrd
ellandrd earned 1000 total points
ID: 19637776
OK here is what im testing and it works for me:

/*
 * Main.java
 *
 * Created on 06 August 2007, 13:19
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package javaapplication1;

/**
 *
 * @author sdelaney
 */
public class Main {
   
    /** Creates a new instance of Main */
    public Main() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            String filename ="W:/Java/JavaApplication1/raymond questions.txt";
            Runtime rt = Runtime.getRuntime();
            final Process proc = rt.exec(
                new String[] {
                "cmd.exe",
                "/c",
                "\"\""+filename+"\"\""
            }
);
        } catch(Throwable cause) {
        }
    }
}
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20213333
Forced accept.

Computer101
EE Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month19 days, 12 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question