Results 1 to 8 of 8
- 07-24-2011, 05:36 AM #1
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
how to execute command from textfile using runtime exec
Hello im trying to execute a command from reading a text file. first my application read the content of textfile and the command from textfile will be executed using Runtime.getRuntime()
but it doest not work.
see my code.
Java Code:public class Main_exec { public static void main(String[]args){ try{ //runtime test #1 // Runtime.getRuntime().exec("cmd /c start C:\\image.jpg"); // <- working //runtime test #2 // String command = "cmd /c start C:\\image.jpg"; // <- working // Runtime.getRuntime().exec(command); //runtime test #3 <- not working TextCommand fsd = new TextCommand(); String readtextfile = fsd.ReadFile(); System.out.println(readtextfile); javax.swing.JOptionPane.showMessageDialog(null,readtextfile); Runtime.getRuntime().exec(readtextfile); //<--not working // Runtime.getRuntime().exec("".concat(readtextfile)); //<--not working }catch(Exception ex){ } } }
Java Code:import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; class TextCommand { private static Reader rd; public static String ReadFile() throws IOException{ File execute = new File("C:\\cmd.txt"); rd = new FileReader(execute); String s = ""; BufferedReader bfr = new BufferedReader(rd); String s2 = ""; while((s = bfr.readLine())!=null){ s2+=s; } rd.close(); bfr.close(); System.out.println("from textCommand"+s2); return s2; } }
content = "del image.jpg"
thanks in advance
- 07-24-2011, 12:25 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Make the contents of that file:
Java Code:cmd /c del image.jpg
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 07-24-2011, 04:47 PM #3
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
- 07-24-2011, 05:09 PM #4nothings happen.
- 07-24-2011, 05:51 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
That is kind of vague; does the String come through when you read it from your file? Does the file exist before you execute that command? Can the process be started? As Norm already suggested: does the process complete with an error? Those are all things you have to check before you come here and complain that nothing happens.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 07-25-2011, 07:43 PM #6
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
looks like my computer is infected by a virus no error message from my Process and no output ithink "cmd /c" is working
Java Code:Process pr = Runtime.getRuntime().exec(readtextfile); BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream())); String s = ""; while((s=bfr.readLine())!=null){ System.out.println(s); }
- 07-25-2011, 08:34 PM #7
To check your java code, try starting notepad.
To check the syntax of you command, try it in a command prompt window.
- 08-06-2011, 09:48 PM #8
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
Similar Threads
-
question with command prompt and Runtime.exec()
By mosquetero in forum New To JavaReplies: 3Last Post: 08-11-2009, 01:08 PM -
runtime.exec
By cotede2 in forum Advanced JavaReplies: 3Last Post: 04-17-2009, 06:18 PM -
help with "Runtime.getRuntime().exec(this.command);"
By itaipee in forum New To JavaReplies: 6Last Post: 12-29-2008, 03:47 PM -
help with Runtime.exec()
By Lanfear in forum New To JavaReplies: 18Last Post: 12-16-2008, 12:09 PM -
Runtime.exec()
By hknyo in forum Advanced JavaReplies: 2Last Post: 08-16-2008, 01:40 AM
Bookmarks