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(filena mes[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" );
}}}
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(filena
// String cmmd = "filecrypt --encrypt "+ filenames[i] + " --user PublicKeyName";
//System.out.println(cmmd)
Process p = Runtime.getRuntime().exec(
}}}
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
Process p = Runtime.getRuntime().exec(
though by the looks start is unecessary and can be removed.
I'd also include the full path to your executable
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
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
ASKER
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(filenam es[i]);
Process p = Runtime.getRuntime().exec( command);
}
}
}
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(filenam
Process p = Runtime.getRuntime().exec(
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html