1

I want to call cmd command in java code. I say:

String str ="C:/uploaded_files/111.txt";
Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c",str});
System.out.println(str);

And dont get 111.txt. Its strange because when this code was in jsp all works fine. What can be wrong?

9
  • 1
    What outcome would you expect from running the program, and what do you actually get? Commented Dec 25, 2012 at 9:01
  • it might be related with file writing permissions. Commented Dec 25, 2012 at 9:01
  • Follow the same advice I gave in comment to this answer. Commented Dec 25, 2012 at 9:04
  • @Gustav Grusell Hmm i want to get 111.txt in C:/uploaded_files folder. But folder still empty. Commented Dec 25, 2012 at 9:04
  • Well he most likely got just the String printed. He never asked for the process output. Commented Dec 25, 2012 at 9:04

3 Answers 3

3

what's the problem with this code. It's perfectly working. opens and shows content of the file 111.txt

try {
    String str ="C:/uploaded_files/111.txt";
    Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c",str});
    System.out.println(str);
    } catch (Exception ex) {}

please check whether the path is correct and whether the directories and files are not missed or spelled

Sign up to request clarification or add additional context in comments.

Comments

0

I hope It is not cmd.exe Please try this:

String[] command = new String[3];
command[0] = "cmd";
command[1] = "/c";
command[2] = "C:/uploaded_files/111.txt";

Process p = Runtime.getRuntime().exec (command);

1 Comment

What you mean I hope It is not cmd.exe?
0

If you want to open the file in notepad try this.

String file = "C:/uploaded_files/111.txt";

Runtime.getRuntime().exec("cmd", "/c", "notepad.exe", file);

Hope it's the one you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.