Avatar of mkhan900
mkhan900 asked on

Execute a CommandLine Program from Java

I am trying to encrypt files through a java program. I am required to call a command line program to do this.
I am not sure if this is correct, please help

public class enCryptFiles
{
public static void main(String args[]) throws Exception
{
    String[] filenames;
    File f = new File(args[0]);
    filenames = f.list();
      for(int i=0; i< filenames.length; i++)
        {System.out.println(filenames[i]);
        // String cmmd = "filecrypt --encrypt "+ filenames[i] + " --user PublicKeyName";
         //System.out.println(cmmd);
           Process p = Runtime.getRuntime().exec("cmd /c start filecrypt --encrypt "+ filenames[i] + " --user PublicKeyName" );
   }}}    

Java

Avatar of undefined
Last Comment
Mick Barry

8/22/2022 - Mon
CEHJ

That's OK. You probably need to handle the process' streams though. Read this carefully

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Mick Barry

try:

           Process p = Runtime.getRuntime().exec(new String[] { "cmd", "/c", "start", "filecrypt", "--encrypt", "filenames[i]", "--user", "PublicKeyName"} );

though by the looks start is unecessary and can be removed.
I'd also include the full path to your executable
Mick Barry

and make sure you include the required command line options so the app does not prompt you for any input (or you will need to provide it) and run silently without any output.
Keeps the code simpler and avoids need to add the code shown in these examples
http://exampledepot.com/egs/java.lang/WriteToCommand.html
http://exampledepot.com/egs/java.lang/ReadFromCommand.html
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
mkhan900

Objects, I tried doing this. I tried printing the array variable, seems like variable "command" isn't getting assigned with the string.
Thanks for the help

public class enCryptFiles
{
      public static void main(String args[]) throws Exception
      {
         String[] filenames;
          File f = new File(args[0]);
          filenames = f.list();
          for(int i=0; i< filenames.length; i++)
        {
           String[] command = new String[] { "cmd", "/c", "start", "filecrypt", "--encrypt"+ filenames[i] + "--user", "PublicKeyName"};
         System.out.println(command);
         System.out.println(filenames[i]);
         Process p = Runtime.getRuntime().exec(command);
        }
    }
}    
ASKER CERTIFIED SOLUTION
Mick Barry

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question