Click here to Skip to main content
15,891,423 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.
===================================
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

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()));
writer.write("help");
writer.flush();

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

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

System.out.println("After while of reader");
reader.close();
stdError.close();
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) {
e.printStackTrace();
}

}

}
==================================
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.
Posted
Updated 2-May-18 22: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