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?
Sri Harsha Chilakapati
12k6 gold badges54 silver badges91 bronze badges
asked Dec 25, 2012 at 8:59
Kliver Max
5,32725 gold badges102 silver badges157 bronze badges
3 Answers 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
answered Dec 25, 2012 at 9:09
Nidhish Krishnan
20.8k6 gold badges67 silver badges78 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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);
answered Dec 25, 2012 at 9:06
Harish Raj
1,5853 gold badges17 silver badges28 bronze badges
1 Comment
Kliver Max
What you mean
I hope It is not cmd.exe?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.
answered Dec 25, 2012 at 9:07
Sri Harsha Chilakapati
12k6 gold badges54 silver badges91 bronze badges
Comments
lang-java
C:/uploaded_filesfolder. But folder still empty.