I have problem in running an exe file with java.
exe file process is like this : after run a command it ask for
password and when i insert password it does the job.
but in my java program I trying to run some commands with an exe file.
i invoke exe file an run my first command and after that i insert password with outputstream but t console get freez and it does nothing.
my code:
String command = "xxxx";
Process p = Runtime.getRuntime().exec(command);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(p.getOutputStream()));
writer.write("123456");
writer.close();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println(" error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
Related
I want to use this script in my java app: https://github.com/jcapona/amazon-wishlist-scraper.
I've looked around and I tried executing the script like so:
public static void main(String[] args) throws IOException {
String s = null;
Process p = Runtime.getRuntime().exec("python C:\\\\Users\\\\Home\\\\work\\\\test.py");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((s=stdInput.readLine()) != null) {
System.out.println(s);
}
}
But I am not getting any output. What do I need to be able to run this specific script?
You can use like this :
String command = "python /c start python path\to\script\script.py>";
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
while ((line = stdInput.readLine()) != null) {
System.out.println(line);
}
stdInput.close();
while ((line = stdErr.readLine()) != null) {
System.out.println(line);
}
stdErr.close();
p.waitFor();
System.out.println("Done.");
p.destroy();
Protip: Always copy your path from the file explorer tab
And since you are getting JSON response try GSON library to parse it.
And if you want to work heavily on python using java, try exploring Jython
I executed this code:
Process proc = Runtime.getRuntime().exec("cat /home/uhf/metrics.sh");
System.out.println(proc.toString());
String proc1 = proc.toString();
But I am not able to get the content of kafka_metrics.sh. Instead I am getting java.lang.UNIXProcess#5fd0d5ae as output. What should I include so that I can get the content of that file?
May this is what you are looking for.
Process proc = Runtime.getRuntime().exec("cat /home/uhf/metrics.sh");
String s = null;
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
You are printing the Process object.
to get the content of the executed process you need to take the input stream and pass that stream to the reader
Process process = Runtime.getRuntime().exec("cat /home/uhf/metrics.sh");
InputStream is = process.getInputStream();
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
br.lines().forEach(System.out::println);
}
In the Command Prompt(cmd) of the Windows OS:
C:\>execution.exe --login user
Enter password:
Success
The work flow is entering the command->system returns message "Enter password:"->Input password->system returns message "Success".
How to do that in my code?
Here is mine, but It doesn't work.
Process p = Runtime.getRuntime().exec("execution.exe --login user");
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(p.getOutputStream()));
writer.write("mypassword\n");
writer.flush();
writer.close();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("System returns message:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("System returns error message:\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
I have some problems using the java ProcessBuilder.
I want to get my gpg keys, so I use the following code:
ProcessBuilder builder = new ProcessBuilder("C:\\[path]\\GnuPG\\pub\\gpg.exe", "--list-keys");
//builder.directory(new File("C:\\[path]\\GnuPG\\pub\\"));
Process process = builder.start();
Scanner s = new Scanner(process.getInputStream()).useDelimiter("\\Z");
System.out.println(s.next());
s.close();
But I always get a NoSuchElementException when executing s.next().
If I use the gpg command "-h", I always get the expected output.
If I change the Constructor call to
new ProcessBuilder("cmd", "/c", "C:\\[path]\\GnuPG\\pub\\gpg.exe", "--list-keys");
it sometimes works. But most times it doesn´t.
WHY? Can anyone help? THANKS!!
Try this-
Test test = new Test();
List<String> commands = new ArrayList<String>();
commands.add("C:\\[path]\\GnuPG\\pub\\gpg.exe");
commands.add("--list-keys");
test.doCommand(commands);
}
public void doCommand(List<String> command)
throws IOException
{
String s = null;
ProcessBuilder pb = new ProcessBuilder(command);
Process process = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader
(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader
(process.getErrorStream()));
StringBuffer start= new StringBuffer();
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
start.append(s);
System.out.println(s.toString());
}
stdInput.close();
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
start.append(s);
System.out.println(s);
}
}
I'm trying to run an external program in java like this:
Process p = Runtime.getRuntime().exec("./shufflet 1 2 <in.seq> out.seq");
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null) {
System.out.println(line);
}
bre.close();
p.waitFor();
Basically, this program that I'm executing (Shufflet) reads in whatever is in in.seq and then writes something to out.seq based on that.
If I copy+paste that line (./shufflet 1 2 <in.seq> out.seq) to the command line it works fine.
If I execute the java program it outputs Usage: shufflet [OPTIONS] NSEQ ORDER <INFILE >OUTFILE which is the error message that Shufflet gives if the parameters are wrong.
I know the parameters are correct because, again, it works if I copy+paste it to the command line.
Any ideas?
Have you tried with DataInputStream ?
DataInputStream myStream = new DataInputStream(p.getInputStream());
while ((line = myStream.readLine()) != null) {
System.out.println(line);
}