Results 1 to 8 of 8
  1. #1
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default 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;
        }
    }
    text filename = "cmd.txt"
    content = "del image.jpg"

    thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Make the contents of that file:

    Java Code:
    cmd /c del image.jpg
    The 'del' command is internal to your command processor cmd.exe

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Java Code:
    cmd /c del image.jpg
    working but if i tried a command with path like this
    Java Code:
    cmd /c del c:\image.jpg
    
    or
    
    cmd /c del c:\\image.jpg
    nothings happen.
    Last edited by Jhovarie; 07-24-2011 at 03:54 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    nothings happen.
    Try reading the Process's error stream to see if there are any error messages

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Jhovarie View Post
    nothings happen.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    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);
                }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    To check your java code, try starting notepad.
    To check the syntax of you command, try it in a command prompt window.

  8. #8
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    i found the solution now. i just reformat my computer because it i infected by the virus. "cmd /c" is working.

Similar Threads

  1. question with command prompt and Runtime.exec()
    By mosquetero in forum New To Java
    Replies: 3
    Last Post: 08-11-2009, 12:08 PM
  2. runtime.exec
    By cotede2 in forum Advanced Java
    Replies: 3
    Last Post: 04-17-2009, 05:18 PM
  3. Replies: 6
    Last Post: 12-29-2008, 02:47 PM
  4. help with Runtime.exec()
    By Lanfear in forum New To Java
    Replies: 18
    Last Post: 12-16-2008, 11:09 AM
  5. Runtime.exec()
    By hknyo in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2008, 12:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •