Click here to Skip to main content
15,792,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
While running below mentioned code in windows it work if we pass the date command and then pass parameter. But for FTP it can't put next command as it is going to FTP prompt and process is just waiting there. I need a way to pass another command on same runtime instance.

public class TestCommand {

public static void main(String[] args) {
int exitValue = -1;
String command = "cmd /c ftp";//"cmd /c date";//
String now = "bye"; //"05-01-2018";//
byte[] b = null;
try {
Process process = Runtime.getRuntime().exec(command);

BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(process.getOutputStream()));

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

System.out.println("After BufferedReader");
String errorline = "";
while ((errorline = stdError.readLine()) != null) {

System.out.println("After checking error");
String line = "";
while ((line = reader.readLine()) != null) {

System.out.println("After while of reader");
System.out.println("Before waitFor");
exitValue = process.waitFor();
System.out.println("After waitFor");
if (exitValue != 0) {
System.out.println("Abnormal process termination");
System.out.println("Finished main execution...");
} catch (IOException | InterruptedException e) {


I need to login in Kerberos using keytab path and then Kerberos prompt come where need to pass Kerberos command and after successful execution need to get out of the Kerberos prompt.

So 1st will put command in bash shell prompt which will bring Kerberos prompt.
Then execute Kerberos command.
Next command to get out of Kerberos prompt.

The same behavior happen for FTP as well. Could not find any java package to handle this scenario. Already tried ChannelExec and ProcessBuilder but they don't work for second inner prompt command.

What I have tried:

I have tried above mentioned code which worked with simple command and parameter but didn't work where first command goes to inner prompt (like FTP prompt or Kerberos prompt) and then next command or parameter does not work.
Updated 2-May-18 23:13pm

1 solution

Put all you commands into a text file and then use the -s option to input them into ftp.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900