Results 1 to 3 of 3
  1. #1
    zoey is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default Run bash command from JAVA on MAC.

    import java.io.File;
    import java.io.IOException;

    public class ReadAllFilesinFolders {
    public static File folder = new File("/Users/path/Documents/test");

    public static void main(String[] args) {
    Runtime myRunTime;
    Process myProcess;
    String myEmail;
    String[] myCmd = {"/bin/bash", "/bin/ls"};

    File[] listOfFiles = folder.listFiles();
    //read all files in a directory
    for (File file : listOfFiles) {
    if (file.isFile()) {
    myEmail = file.getName();
    myRunTime = Runtime.getRuntime();
    myProcess = myRunTime.exec(myCmd);

    }
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Run bash command from JAVA on MAC.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Did you have a question?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    zoey is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default Re: Run bash command from JAVA on MAC.

    Never mind my previous quesiton. Here is my code that worked without compiler errors. However, the Runtime.exe(myCmd) didn't execute my commands. I have tried with simple commands, such as "ls", it didn't work either. Is it something to do with the environment setting? I knew that systemIO needs to be redirected. But I don't know how. My program is running on jGrasp on MAC OS X.

    Java Code:
    import java.io.File;
    //import java.io.BufferedReader;
    import java.io.IOException;
    //import java.io.InputStreamReader;
    //import java.util.ArrayList;
    import java.util.List;
    
    public class ReadAllFilesinFolders {
      public static File folder = new File("/Users/sscoggins/Documents/MITRE_PROJECTS/FBI/EmailSample/Sophia_Email");
      
      public static void main(String[] args) {
        Runtime  myRunTime;
        Process  myProcess;
        String   myEmail;
        //String[] myCmd = {"munpack"};
        String[] myCmd = {"munpack", "-ft", "*.eml"};
        //String[] myPar = {"-ft", "*.eml"}; 
    
    
        System.out.println("Reading files under the folder "+ folder);
    
        File[] listOfFiles = folder.listFiles();
        //read all files in a directory
        for (File file : listOfFiles) {
           if (file.isFile()) {
              myEmail = file.getName();
    
              //myCmd = "munpack -ft "+ myEmail+"";
    
              //myPar[1]=myEmail;
              myCmd[2] = myEmail;
              //System.out.println(myCmd[0]+" "+myPar[0]+" "+myPar[1]);
              System.out.println(myCmd[0]+" "+myCmd[1]+" "+myCmd[2]);
              myRunTime = Runtime.getRuntime();
              try {
                 //myProcess = myRunTime.exec(myCmd, myPar);
                 myProcess = myRunTime.exec(myCmd, null);
    
                 
                 System.out.println("All good");
              }
             catch (IOException e) {
                System.out.println(e);
                
              }
              //myProcess.waitFor();        
              
           }
        } 
      }
    }

Similar Threads

  1. Can I pass a Java variable to bash script?
    By steve-ss in forum New To Java
    Replies: 2
    Last Post: 05-30-2012, 10:18 PM
  2. Java exit command...
    By sh4rif in forum New To Java
    Replies: 7
    Last Post: 01-18-2012, 12:41 AM
  3. [Help Please] Java [Rsps Command]
    By omfgimawesome in forum New To Java
    Replies: 12
    Last Post: 07-17-2011, 02:43 PM
  4. Letting my program command the command prompt
    By Smokey. in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-29-2011, 11:34 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Tags for this Thread

Posting Permissions

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