I am trying to invoke a command prompt and logging in some credentials into it. For this I am taking following approach:
public static void main(String[] args) throws InterruptedException {
// init shell
ProcessBuilder builder = new ProcessBuilder("cmd");
Scanner scanner = null;
BufferedWriter writer = null;
try {
scanner = new Scanner (System.in);
System.out.print("Enter your MS ID : ");
String user = scanner.nextLine();
System.out.print("Enter your MS Password : ");
String pass = scanner.nextLine();
Process p = builder.start();
writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("oc logout");
writer.newLine();
writer.flush();
writer.write("oc login <private-url>");
writer.newLine();
writer.flush();
writer.write(user);
writer.newLine();
writer.flush();
writer.write(pass);
writer.newLine();
writer.flush();
//Writing this will end the process after login is done
// writer.write("exit");
// writer.newLine();
// writer.flush();
Thread t = new Thread(new Runnable() {
public void run() {
try(BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
;
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
});
t.setDaemon(true);
t.start();
p.waitFor();
} catch (IOException e) {
System.out.println(e);
}
finally {
scanner.close();
}
}
But when I run the program, it just does not end. Eclipse and running by cmd always need to explicitly exit the program. Maybe I am missing something. Any help would be greatly appreciated. It just works for the desired thing but does not exit and I need to integrate in my program and not able to do so.
You don't close the writer!
Your loop in the runnable task asks the reader whether it has more input:
while ((line = br.readLine()) != null) { ... }
This is always the case unless the writer in the main thread is closed. So for a quick solution put a simple writer.close() after the last writing action.
The better solution is to use the try-with-resources statement, introduced with Java 7. You should open your readers, writers, scanners, etc. as following:
try (Scanner scanner = ...) { ... }
try {Reader reader = ...) { ... }
try {Writer writer = ...) { ... }
These statements will handle the closing automatically for you. And as a side effect, it will make your code much more readable.
Warning: Closing a Scanner which is connected with System.in also closes the standard input, so that after that point no input can be read anymore. If that is not appropriate in your part of the code, then do not close the scanner.
Related
I am building a server in Java, and the input feed is giving me trouble - its current setup cannot read more than one line, and it cannot read anything without "\n" at the end. Here is the current input loop:
BufferedReader input = new BufferedReader(new InputStreamReader(serverConnection.getInputStream()));
if(input.ready()){
StringBuilder text = new StringBuilder();
String line;
while((line = input.readLine()) != null){
text.append(line);
System.out.println(line);
}
sentData = text.toString();
return true;
}
My current loop using a BufferedReader cannot read more than one line of incoming data, and if there is no newline character, a timeout exception is thrown (I programmed it to do that), and the data is not read at all. It is, of course, unacceptable to have a listener that can only see one line of data, and stalls when the sent data is not formatted properly.
So I am looking for a method that allows the program to read any number of lines, and for the program to stop reading when it has reached the end of the data stream (even without a new line).
Any help is appreciated.
You're throwing away every odd-numbered line. Remove the readLine() inside the loop.
You can read from the InputStream directly into the StringBuilder. readLine() of BufferedReader will wait for \r or \n character. This looks like stalling.
int ch;
StringBuilder text = new StringBuilder();
while((ch = serverConnection.getInputStream().read())!= -1) { // -1 will be read at EOS
text.append((char)ch);
}
sentData = text.toString();
return true;
Update
The following piece of code is to demonstrate the difference between usage of BufferedReader and InputStream to read bytes and what is available to the user during read operation. BufferedReader will always give you lines which are either terminated by line breaks or by EOS. Whereas InputStream will make the available bytes to the user.
In scenarios where it is NOT necessary to close the streams, and the bytes transferred has it's own way to mark start and end of packets/messages, you will be using InputStream to read bytes. If you try using BufferedReader for these applications, the last line of the message will be made available once the server receives the next packet/message, unless you send each line with a line-break.
public static void main(String[] args) throws Exception {
new Thread(new BufferedReaderServer()).start();
new Thread(new InputStreamServer()).start();
final String requestString = "Line#1\nLine#2";
System.out.println("\nSending to BufferedReaderServer");
Socket clientSocket1 = new Socket(InetAddress.getLocalHost()
.getHostAddress(), 8003);
OutputStream outputStream1 = clientSocket1.getOutputStream();
outputStream1.write(requestString.getBytes());
Thread.sleep(6000);
outputStream1.close();
clientSocket1.close();
Thread.sleep(1000);
System.out.println("\nSending to InputStreamServer");
Socket clientSocket2 = new Socket(InetAddress.getLocalHost()
.getHostAddress(), 8004);
OutputStream outputStream2 = clientSocket2.getOutputStream();
outputStream2.write(requestString.getBytes());
Thread.sleep(6000);
outputStream2.close();
clientSocket2.close();
}
static class BufferedReaderServer implements Runnable {
public void run() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8003);
Socket socket = serverSocket.accept();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String s = null;
System.out.println("BufferedReaderServer read START");
while ((s = bufferedReader.readLine()) != null) {
System.out.println(s);
}
System.out.println("BufferedReaderServer read END");
socket.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
static class InputStreamServer implements Runnable {
public void run() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8004);
Socket socket = serverSocket.accept();
System.out.println("InputStreamServer read START");
int ch;
while ((ch = socket.getInputStream().read()) != -1) {
System.out.print((char) ch);
}
System.out.println("\nInputStreamServer read END");
socket.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
The issue what is being faced here could be due to non-closing of client socket. Depends on the user's application
I am trying to allow communication between one program (the program launcher, if you will) and the programs it launches via processbuilder. I have the output working fine, but the input seems to stop when it reaches the readline() method in helloworld (the created process).
Below is helloworld.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class helloworld {
public static void main (String[] args) {
System.out.println ("println(\"Hello World!\")");
System.out.println ("getInput()");
Scanner in = new Scanner(System.in);
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
String input = "";
try {
// wait until we have data to complete a readLine()
while (!br.ready()) {
Thread.sleep(200);
}
System.out.println("println(\"Attempting to resolve input\")");
input = br.readLine();
^This is where program hangs^
if(input != null){
System.out.println("println(\"This should appear\")");
}
System.out.println("println(\"input recieved " + input + "\")");
} catch (InterruptedException | IOException e) {
System.out.println("ConsoleInputReadTask() cancelled");
}
System.out.println("println(\"You said: " + input + "\")");
//System.out.println("println(\"You said: " + in. + "!\")");
in.close();
System.exit(0);
}
}
This is where the output (println) from the other process is recieved:
public void run() {
try {
//cfile = files[indexval].getAbsolutePath();
String[] commands =
{
"java", //Calling a java program
"-cp" , //Denoting class path
cfile.substring(0,cfile.lastIndexOf(File.separator) ), //File path
program}; //Class name
ProcessBuilder probuilder = new ProcessBuilder( commands );
//start the process
Process process = probuilder.start();
//Read out dir output
//probuilder.inheritIO(); //Can inherit all IO calls
InputStream is = process.getInputStream();
OutputStream os = process.getOutputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
BufferedReader br = new BufferedReader(isr);
String line;
/*System.out.printf("Output of running %s is:\n",
Arrays.toString(commands));*/
while ((line = br.readLine()) != null) {
myController.runCommand(line, "Please enter something!", bw);
//System.out.println(line);
}
br.close();
os.close();
} catch (IOException e){
e.printStackTrace();
}
System.out.println("programclosed");
}
And here is the function that it calls:
public synchronized void runCommand(String line, Object... arguments) throws IOException {
String[] tokens;
if(line.contains("(")){
tokens = line.split("\\(",2);
switch(tokens[0]){
case "println": //Println - format println(String strng)
tokens[1] = tokens[1].substring(1, tokens[1].length() - 2);
System.out.println(tokens[1]);
break;
case "getInput": //Get input - format getInput(String command, String message, BufferedWriter br)
Scanner reader = new Scanner(System.in);
System.out.println(arguments.length);
System.out.println(((String)arguments[0]));
BufferedWriter in = ((BufferedWriter)arguments[1]);
in.write(reader.nextLine());
System.out.println("sending input");
in.flush();
reader.close();
break;
default:
System.out.println("Invalid command recieved!");
}
} else
System.out.println("Invalid command recieved!");
}
The output I recieve is:
Hello World!
2
Please enter something!
This is a test input
sending input
Attempting to resolve input
As you can see, I successfully exit the while(!br.ready()) loop, and I stop at br.readLine();
I am aware inheritIO() exist, but for this case I am using the BufferedOuput to send commands which are then parsed and sent to the switch statement, which in turn calls the corresponding function. This is because multiple processes could be launched from the process manager, think of the fun when multiple System.in calls arrive, with nothing to determine which process it is for! In addition, this allows for me to call any type of function, even those not related to println or input.
I believe the issue here is a result of the following:
BufferedReader.ready() returns true if there are any characters available to be read. It does not guarantee that there are any carriage returns among them. (docs)
BufferedReader.readLine() looks for a carriage return to complete a line. If one is not found, it blocks.
BufferedWriter.write() does not automatically write a terminating carriage return.
To test whether this is actually the problem, replace this line in runCommand():
in.write(reader.nextLine());
with:
in.write(reader.nextLine() + "\n");
I tried to make a console app that creates a file and writes text to it. What did I do wrong here?
package Writer;
import java.util.*;
import java.io.*;
public class main {
public static void main(String args[]) {
System.out.println("What would you like your SimpleText file name to be?");
Scanner uInputName = new Scanner(System.in);
String fileName = uInputName.nextLine();
File file = new File(fileName);
FileWriter fw = FileWriter;
BufferedWriter Text = new BufferedWriter(fw);
{
System.out.println("What would you like to write in: " + fileName);
Scanner uInputText = new Scanner(System.in);
String fileText = uInputText.nextLine();
fw.write(fileText);
Text.newLine();
}
System.out.println("Okay. File saved.");
}
This is totally illegal
FileWriter fw = FileWriter;
You probably want this
BufferedWriter Text = new BufferedWriter(new FileWriter(file));
You have an unnecessary code block
{
}
Your code is going to throw an IOExcpeption, so you have two options. Either
public static void main(String[] args) throws IOException {
}
Or
try {
BufferedWriter Text = new BufferedWriter(new FileWriter(file));
....
Text.write(inputText);
Text.close();
} catch (IOException ex) {
ex.printStackTrace();
}
First, let us begin with FileWriter fw.
fw should be a FileWriter object and so, you need to create a FileWriter object.
FileWriter fw = new FileWriter(file);
Now, notice that you may want to be more specific so it's worth checking the different constructors in the FileWriter API
Also, the writing won't THROW and exception, it MIGHT THROW an exception (saying that it will throw an exception is saying that your code will always fail and in that case, exception handling is "patching" it.
So I would recommend adding a catch clause. However, remember that YOU must ALWAYS close the writers yourself (Java does not do that unless specified otherwise) and so, you need to either try with resource
try (FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw)){
....
writer.write(inputText);
} catch (IOException ex) {
ex.printStackTrace();
}
or, add a finally clause
FileWriter fw = new FileWriter(file);
BufferedWriter writer = new BufferedWriter(fw);
try {
....
writer.write(inputText);
} catch (IOException ex) {
ex.printStackTrace();
} finally{
writer.close();
fw.close();
}
Because the garbage collector does not close streams and if the exception is thrown inside the try then it will not reach the close() command but instead go to catch (and then finally) without closing the stream.
As others have said. it is difficult to know all the things that are wrong without knowing what exactly you are trying to accomplish.
And again, as PopoFibo mentioned, the thing that probably stands out most and seems to be syntactically incorrect is:
FileWriter fw = FileWriter;
So instead of listing what might be wrong, here is an example that might help you.
In first line you you enter in console you specify the file name - like you seem to want to do.
Each subsequent line you input into console is written to that file. Entering empty line will terminate the input.
public class Program {
public static void main(String[] args) {
System.out.println("Enter file name:");
Scanner input = new Scanner(System.in);
String fileName = null;
while (fileName == null) {
if (input.hasNextLine()) {
fileName = input.nextLine();
}
}
try {
FileWriter fw = new FileWriter(fileName);
String newLine = System.getProperty("line.separator");
System.out.println("Enter lines (enter empty line to terminate):");
String line = null;
while (true) {
if (input.hasNextLine()) {
line = input.nextLine();
if (isTerminatingEntry(line)) {
break;
}
fw.write(line + newLine);
}
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
input.close();
}
private static boolean isTerminatingEntry(String line) {
// add some condition that will cause you to stop reading input
// in this example, empty line will terminate the input
return line.length() == 0;
}
}
Im using java to start a GNOME terminal process in linux and redirecting its input and output to my code. Below is a code.
public static void main(String[] args) throws InterruptedException
{
// TODO Auto-generated method stub /usr/bin/telnet
try
{
String line, commandInput;
Scanner scan = new Scanner(new File("/home/Ashutosh/Documents/testfile"));
ProcessBuilder telnetProcessBuilder = new ProcessBuilder("/bin/bash");///home/Ashutosh/Documents/tempScript");
telnetProcessBuilder.redirectErrorStream(true);
Process telnetProcess = telnetProcessBuilder.start();
//Process telnetProcess = Runtime.getRuntime().exec("xterm");///home/Ashutosh/Documents/tempScript");
BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream()));
while(scan.hasNext())
{
commandInput = scan.nextLine();
output.write(commandInput);
/* if(commandInput.trim().equals("exit"))
{
output.write("exit\r");
}
else
{
output.write("((" + commandInput + ") && echo --EOF--) || echo --EOF--\r");
}
*/ output.flush();
line = input.readLine();
while(line != null && !line.trim().equals("--EOF--"))
{
System.out.println(line);
line = input.readLine();
}
if(line == null)
break;
}
/* Thread.sleep(500);
output.write("/home/Ashutosh/Documents/testfile\r");
Thread.sleep(500);
output.flush();
while((line = input.readLine())!= null)
System.out.println(line);
telnetProcess.destroy();
*/ //String s = input.readLine();
//System.out.print(s + "\r\n");
//s = input.readLine();
//System.out.print(s + "\r\n");
}
the contents of testfile which is bash script is
#!/bin/bash
ls -l
pwd
date
exit
and i also tried the following interactive script which takes input from user which i want to redirect from java code is given
#! /bin/bash
echo "Input any number form 0-3:"
read num
case $num in
0) echo "You are useless";;
1) echo "You are number 1";;
2) echo "Too suspecious";;
3) echo "Thats all man, got to go...!";;
*) echo "Cant't u read english, this is wrong choice";;
esac
read
exit
my code stops at input.readLine(); im not sure but i think i am not able to redirect the output stream
output.write(commandInput);
command is executing well but did not write i intended to on the process redirected input, that is why it hangs at readLine();.
If somebody have already solved please let me know the solution.
From following link i tried to solve the issue but still no luck:
Java Process with Input/Output Stream
Thanks
Ashutosh
readLine() read the contents of a line, without the newline at the end.
write() writes just the text, it doesn't add a new line.
Perhaps you want to add write("\n"); or use PrintStream or PrintWriter.
I imagine, your script is being sent as
#!/bin/bashls -lpwddateexit
which is a comment without a newline.
EDIT: Huge error, not adding the command to the ProcessBuilder!!
Why are you not just running your script as a Linux script? That is,
ProcessBuilder builder = new ProcessBuilder();
LinkedList<String> command = new LinkedList<String>();
command.add("/bin/bash");
command.add("/home/Ashutosh/Documents/testfile");
builder.command(command);
Process myProc = builder.start();
Also, I notice the variable is named 'telnetProcess' yet there is no invocation of telnet anywhere that I can see. Perhaps this is the problem?
EDIT: Added my suggestion below for the interactive script.
ProcessBuilder builder = new ProcessBuilder();
LinkedList<String> command = new LinkedList<String>();
command.add("/bin/bash");
command.add("/path/to/interactiveScript");
builder.command(command);
final Process myProc = builder.start();
// Java input goes to process input.
Thread inputParser = new Thread() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(myProc.getOutputStream()));
String line = "";
while(line != null) {
line = br.readLine();
bw.write(line);
bw.newLine();
}
}
}.start();
// Process output must go to Java output so user can see it!
Thread outputParser = new Thread() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(myProc.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = "";
while(line != null) {
line = br.readLine();
bw.write(line);
bw.newLine();
}
}
}.start();
hi guys sorry for late response, after some trials i got it working. I am simply letting the process completing its process and exit normally rather than forcefully and then the BufferedReader and BufferedWriter keeps the string buffers in RAM which i can read now after process exit with code 0.
public static void main(String[] args) throws InterruptedException
{
// TODO Auto-generated method stub
try
{
String line, commandInput;
ProcessBuilder telnetProcessBuilder = new ProcessBuilder("/bin/bash");
telnetProcessBuilder.redirectErrorStream(true);
Process telnetProcess = telnetProcessBuilder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream()));
commandInput = "ls -l\n";
output.write(commandInput);
output.flush();
commandInput = "pwd\n";
output.write(commandInput);
output.flush();
commandInput = "whoami\n";
output.write(commandInput);
output.flush();
commandInput = "exit\n";
output.write(commandInput);
output.flush();
while((line = input.readLine())!= null)
System.out.println(line);
}
}
Can someone help me in the below scenario,
I need to call a perl script from my java code. The perl script is an interactive code, which gets the input from the user during its execution and continues further to end. So, the example I have used is, the perl script when executed asks for the age by printing in the console "How old are you?", when the user enter some value say '26'. Then it prints "WOW! You are 26 years old!".
When I tried calling this script from my java code, the process waits till I give the value as 26 in the outputstream, while in the inputstream there is no value. Then finally when again I read the inputstream, i get the entire output of the script together. So, here can't I make it interactive?
I have went through many forums and blogs, but couldn't locate any, which exactly target my requirement.
Here is the java code
import java.io.*;
public class InvokePerlScript {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Process process;
try
{
process = Runtime.getRuntime().exec("cmd /c perl D:\\sudarsan\\eclips~1\\FirstProject\\Command.pl");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
out.write("23");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
process.waitFor();
if(process.exitValue() == 0)
{
System.out.println("Command Successful");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
System.out.println("Command Failure");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
catch(Exception e)
{
System.out.println("Exception: "+ e.toString());
}
}
}
Perl code is as below
$| = 1;
print "How old are you? \n";
$age = <>;
print "WOW! You are $age years old!";
Thanks in advance,
Sudarsan
Are you calling flush() on the OutputStream in Java after writing the values? If you don't, there's a good chance they'll just be held in the stream's buffer within the Java process, and so never make it to Perl (with the result that both processes end up waiting for the other's IO.)
(Depending on the implementation of the stream this may or may not be necessary, but it certainly wouldn't hurt - and I've been bitten by this in the past. Usually one doesn't need to be as careful, since flushing happens implicitly when close() is called, but here you can't close the stream after you've finished writing.)
It looks like you're trying to read a full line in this code:
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
...
However, in your perl code, you are not printing an endline character, so readLine never returns (as per the documentation).