Process requires redirected input - java

I have a UNIX native executable that requires the arguments to be fed in like this
prog.exe < foo.txt.
foo.txt has two lines:
bar
baz
I am using java.lang.ProcessBuilder to execute this command. Unfortunately, prog.exe will only work using the redirect from a file. Is there some way I can mimic this behavior in Java?
Of course,
ProcessBuilder pb = new ProcessBuilder("prog.exe", "bar", "baz");
does not work.
Thanks!

ProcessBuilder pb = new ProcessBuilder("prog.exe");
Process p = pb.start();
OutputStream pos = p.getOutputStream();
InputStream fis = new FileInputStream("file.txt");
byte[] buffer = new byte[1024];
int read = 0;
while((read = fis.read(buffer)) != -1) {
pos.write(buffer, 0, read);
}
fis.close();
Not tested, but something like this should work.

I ended up doing something like this and it works. Thanks for all the help!
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
public class UserInp {
public static void main(String[] args) {
new UserInp().sample();
}
public void sample() {
String command = "foo.exe";
List<String> args = new LinkedList<String>();
args.add("bar");
args.add("baz");
ProcessBuilder pb = new ProcessBuilder(command);
java.lang.Process process = null;
try {
process = pb.start();
} catch (IOException ex) {
//--
}
OutputStream os = process.getOutputStream();
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
final InputStream is = process.getInputStream();
new Thread(new Runnable() {
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (java.io.IOException e) {
}
}
}).start();
for (String arg : args) {
pw.println(arg);
}
pw.close();
int returnCode = -1;
try {
returnCode = process.waitFor();
} catch (InterruptedException ex) {
//--
}
System.out.println(returnCode);
}
}

The redirection is setup by the shell you need todo this manually, something like that:
Process proc = pb.start();
OutputStreamWriter os = new OutputStreamWriter(proc.getOutputStream());
// then write the data from your file to os
// ...
os.close();

Did you try to wrap prog.exe into a script which accepts arguments and deal with prog.exe ?
I assume you're using a shell, so it's quite simple to set up a bash script.
If I understand your problem, the script would look like :
#!/usr/bin/bash
echo $1 > file
echo $2 >> file
prog.exe < file
remove file

Build the process using a ProcessBuilder, then use process.getOutputStream() to get an OutputStream that will pipe to the standard input of the process.
Open the file using normal Java techniques, read the contents of the file and write it to the OutputStream going to the Process you made with the ProcessBuilder.
The problem you have right now is that you're calling the ProcessBuilder to launch
$ prog.exe foo bar
Which is probably nothing close to what you want to achieve.

Related

WMIC Java command not working after exe installation

A very simple code running in the debugging mode perfectly but not working after installation of exe, giving no response/result in return. even no errors to trace.
After building a .exe and installing on my PC its happening, very strange.
tried process builder but the same thing, anyway to check/trace it. maybe paths ?
StringBuilder b = new StringBuilder();
Process p = Runtime.getRuntime().exec("wmic diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
Please note: CMD /c before commands also return an empty response in actual env.
An internal windows command with arguments, like "wmic diskdrive ..."
can be executed easily by wrapping it up inside a cmd window.
Here is the working code snippet for running the wmic command encapsulated in a cmd window:
import java.io.*;
public class Wmic {
public static void main(String[] args) {
StringBuilder b = new StringBuilder();
try {
// Initialize a process object to run
// wmic command and its parameters
// inside a cmd window
Process process = Runtime.getRuntime()
.exec("cmd /c C:\\Windows\\System32\\wbem\\WMIC.exe diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
} catch (Exception ex) {
b.append(ex.toString());
}
System.out.println("Output: \n" + b.toString());
}
}
Output:
>javac Wmic.java
>java Wmic
Output:
Signature
More information:
https://mkyong.com/java/how-to-execute-shell-command-from-java/
The ProcessBuilder constructor takes a list of strings. When using ProcessBuilder to run a command, I separate all the words in the command into separate strings.
I read the output of the process in a separate thread. And I always wait for the command, that I launched via ProcessBuilder, to terminate.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
public class PrcBldTs {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("wmic","diskdrive","get","signature");
try {
Process proc = pb.start();
StreamGobbler error = new StreamGobbler(proc.getErrorStream());
StreamGobbler output = new StreamGobbler(proc.getInputStream());
Thread stdout = new Thread(output);
Thread stderr = new Thread(error);
stdout.start();
stderr.start();
int result = proc.waitFor();
stdout.join();
stderr.join();
System.out.println("Exit status = " + result);
if (result != 0) {
System.out.println(error.getContents());
}
else {
System.out.println(output.getContents());
}
}
catch (IOException | InterruptedException x) {
x.printStackTrace();
}
}
}
class StreamGobbler implements Runnable {
private BufferedReader reader;
private StringWriter sw;
public StreamGobbler(InputStream is) {
InputStreamReader isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
sw = new StringWriter();
}
public String getContents() {
return sw.toString();
}
public void run() {
try {
String line = reader.readLine();
while (line != null) {
sw.append(line);
sw.append(System.lineSeparator());
line = reader.readLine();
}
}
catch (IOException xIo) {
throw new RuntimeException(xIo);
}
}
}
Running the above code gave me the following output.
Exit status = 0
Signature
1145609371

Some terminal commands run via Java don't display output in Linux

I'm trying to write a Java program to run terminal command. Googling and SO got me to here:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Detector {
public static void main(String[] args) {
String[] cmd = {"ls", "-la"};
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) !=null){
System.out.println(line);
}
p.waitFor();
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
So far, so good. The problem is if I try to run a command like "python -V" via
String[] cmd = {"python", "-V"};
The program will run, but no output is actually printed out. Any ideas?
The output you see on your command line when running python -V is actually being printed to standard error. To capture this, you need to use a different InputStream, such as this:
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(p.getErrorStream()));
The rest of your code is fine.

I cant make the text file as read only mode in java

package com.studytrails.tutorials.springremotingrmiserver;
import java.lang.Object;
import java.awt.Desktop;
import java.io.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class GreetingServiceImpl implements GreetingService
{
#Override
public String getGreeting(String name) {
return "Hello " + name + "!";
}
public String getText() {
ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"spring-config-server.xml"});
Resource resource = appContext.getResource("file:D:\\text\\test.txt");
StringBuilder builder = new StringBuilder();
try {
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
File temp=File.createTempFile("output", ".tmp");
String filePath=temp.getAbsolutePath();
System.out.println(""+filePath);
String tem=temp.getName();
String line;
PrintWriter out = new PrintWriter(new FileWriter(tem));
//System.out.println(""+filePath);
while ((line = br.readLine()) != null) {
out.println(line);
}
temp.setReadOnly();
String[] cmd = {"notepad",tem};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
// proc.getInputStream();
out.close();
br.close();
//temp.deleteOnExit();
}
catch(IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}
In above code I am not able to setReadonly(); command to temp file. File displayed with all options can you suggest how to make the temp file as not modified and even it is not able to save to another location. I need this file only displayed at run time of the program. During that time user does not change any content and it could not be save as another location.
I suggest to close the file before using it (by notepad):
PrintWriter out = new PrintWriter(new FileWriter(tem));
//System.out.println(""+filePath);
while ((line = br.readLine()) != null) {
out.println(line);
}
out.close();
temp.setReadOnly();
String[] cmd = {"notepad",tem};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
// proc.getInputStream();
To avoid that a user move the file to another location you may additional create the temp file in a temp folder and make the folder readonly too. But if user can read they normally can copy the file to another location. If I remember well, in Windows you can mark for read but prevent from copy a file (thru Windows of course).

pipe telnet output to logfile with tee command using Java process exec

Concurrency and stream reading in a project for a simple MUD client is proving a bit of headache, so I'm trying to find an alternative. The tee command looks to fit the bill for concurrently splitting output between a file and the terminal. How do I then send messages through the telnet session?
Splitting remote output between the console and a file:
thufir#dur:~/NetBeansProjects/TelnetConsole$
thufir#dur:~/NetBeansProjects/TelnetConsole$ telnet rainmaker.wunderground.com 3000 | tee out.txt
Trying 38.102.137.140...
Connected to rainmaker.wunderground.com.
Escape character is '^]'.
------------------------------------------------------------------------------
* Welcome to THE WEATHER UNDERGROUND telnet service! *
How do I then pipe or somehow send Java messages to the system telnet client? Or, perhaps, would it make more sense to use exec and start the telnet session, and tee, from within Java? Just looking for a good approach.
Yes, exec sounds like a better solution since you want to control both input and outpud data. And do you really need tee in when you use exec, I don't understand why...
Also take a look at netcat instead of telnet - telnet has some special character handling that could give you trouble if you plan to send binary data.
By no means functioning code, but definitely simpler than using Apache TelnetClient. However, it's not possible to use the pipe command(?) with java exec:
package exec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
class Ideone {
public static void main(String[] args) throws IOException {
new Ideone().start();
}
public void start() throws IOException {
String[] s = new String[6];
s[0] = "telnet";
s[1] = "rainmaker.wunderground.com";
s[2] = "3000";
s[3] = "|";
s[4] = "tee";
s[5] = "out.log";
for (int i=0;i<s.length;i++ ) {
System.out.println(s[i]);
}
Process process = Runtime.getRuntime().exec(s);
OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();
read();
write(stdout);
}
private void parseLog() {
//read the log file, automate responses
}
private void write(InputStream stdin) throws UnsupportedEncodingException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(stdin, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
private void read() {
Thread read = new Thread() {
String command = null;
#Override
public void run() {
BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(System.in));
do {
try {
command = bufferedInput.readLine();
} catch (IOException ex) {
System.out.println(ex);
} finally {
}
} while (true);
}
};
read.start();
}
}

how to execute console commands in a java program for node

I need to access the console on the node through java
how to make this ?
public class Comando {
public static void main(String[] args) {
String comando = "C:\\Program Files\\nodejs\\node.exe";
try {
Process process = Runtime.getRuntime().exec(comando);
OutputStream stdin = process.getOutputStream ();
InputStream stderr = process.getErrorStream ();
InputStream stdout = process.getInputStream ();
stdin.write("1+2".getBytes());
stdin.flush();
// System.out.print(stdout.read());
stdin.close();
System.out.print(stdout.read());
//BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
//BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
// writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I expect that node.exe requires text, not binary as you are using it.
This means that using PrintWriter to write lines of text and BufferedReader to read lines of text would make more sense.
Java has a built in Javascript interpreter. I assume you cannot use that for some reason.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
public class Comando {
public static void main(String[] args) {
String comando = "C:\\Program Files\\nodejs\\node.exe";
try {
ProcessBuilder builder = new ProcessBuilder(comando);
Process process = builder.start();
OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
stdout));
PrintWriter writer = new PrintWriter(stdin);
writer.write("1+2");
writer.flush();
stdin.close();
System.out.print(reader.read());// return -1
// writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
It continues at me return -1, when I try read your output

Categories

Resources