I was trying to print the version of chrome using process builder, this works fine with the command prompt when I executed wmic command directly in the windows command prompt, the same doesn't work with the process builder
String path= "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
String[] cmd = { "CMD", "/C", "wmic datafile where name="+path+" get Version /value" };
ProcessBuilder probuilder = new ProcessBuilder(cmd);
Thread.sleep(5000);
Process p = probuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String readline;
int i = 0;
while ((readline = reader.readLine()) != null) {
System.out.println(++i + " " + readline);
}
}
Expected
It has to print the following output: Version=55.0.2883.87
Suggest some solution
try with
String[] cmd = { "CMD", "/C", "wmic datafile where \"name='"+path+"'\" get Version /value" };
you need the path quoted like:
"name='C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'"
I think even you can execute directly the wmic without the cmd.
edit whole code snipped(double slashes are also needed in the wmic path):
Runtime rt = Runtime.getRuntime();
String path= "C:\\\\Program Files (x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe";
Process p2=Runtime.getRuntime().exec("cmd /C wmic datafile where 'name=\""+path+"\"' get Version ");
BufferedReader reader = new BufferedReader(new InputStreamReader(p2.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p2.getErrorStream()));
String readline;
System.out.println("Output:\n");
while ((readline = reader.readLine()) != null) {
System.out.println(readline);
}
System.out.println("Errors:\n");
while ((readline = stdError.readLine()) != null) {
System.err.println(readline);
}
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 want to run a python script(tensorflow's Image label script) after RPi's camera module captures a photo using a jar file. I have tried both Runtime and ProcessBuilder, but it says no file or Directory found.
Here's my Code for the Runtime Code:
Process rt = Runtime.getRuntime().exec("python3 -m scripts.image-label.py");
rt.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(rt.getInputStream()));
String read = in.readLine();
ML = read;
result resfin = new result();
resfin.setVisible(true);
And here's the code for my ProcessBuilder one:
ProcessBuilder builder = new ProcessBuilder("/home/pi/Desktop/ML/scripts/image-label.py");
Process np = builder.start();
np.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(np.getInputStream()));
String read = in.readLine();
ML = read;
result resfin = new result();
resfin.setVisible(true);
Am i doing something wrong? Or am i missing something? Any help would be appreciated!
I have tried with the following code, it is working fine, though I have not tried with -m "module" flag, since I do not know how to create a module file in python.
Found some of the issues with code:
1. You need to pass python3 as an argument to process builder as shown below
2. Provide an absolute path for a python file.
3. You can use either of Runtime or ProcessBuilder without an issue.
// Process rt = Runtime.getRuntime().exec("python3 -m /Users/<user-name>/demo/JavaNotepad/src/main/java/com/mypython.py");
ProcessBuilder builder = new ProcessBuilder("python3", "-m", "/Users/<user-name>/demo/JavaNotepad/src/main/java/com/mypython.py");
Process rt = builder.start();
int exitCode = rt.waitFor();
System.out.println("Process exited with : " + exitCode);
BufferedReader in = new BufferedReader(new InputStreamReader(rt.getInputStream()));
BufferedReader err = new BufferedReader(new InputStreamReader(rt.getErrorStream()));
System.out.println("Python file output:");
String line;
BufferedReader reader;
if (exitCode != 0) {
reader = err;
} else {
reader = in;
}
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
I am trying to trigger python code from a java scheduler. In standalone mode, we execute the python script via conda prompt, it takes arguments and returns value in JSON. How do I call this from a Java program and capture the output? Please help
You can use the Runtime to execute your command. Check the doc as well.
Here is an example how you can use it:
Process process = Runtime.getRuntime().exec("ls -al");
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String read;
while ((read = br.readLine()) != null) {
sb.append(read);
}
br.close();
System.out.println(sb.toString());
You should be using Java's ProcessBuilder to open a command line and execute your python script. Code should look like as follows (adapted to linux OS so change the command line if you're on Windows):
//create a command line to execute python script
ProcessBuilder builder = new ProcessBuilder("python", "scriptname.py", "param1", "param2", ...);
builder.redirectErrorStream(true);
Process p = builder.start();
//read output
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
//convert output to JSON
Gson gson = new Gson();
Type type = new TypeToken<YourType>(){}.getType();
YourType obj = gson.fromJson(line, type);
I was tried to use ProcessBuilder for open ffplay.exe and execute command into ffplay. However it is unsuccessful. How could I do that?
Code:
ProcessBuilder pb = new ProcessBuilder();
pb.command("C:\\Windows\\System32\\cmd.exe", "/c",
"C:\\ffmpeg\\bin\\ffplay.exe", "tcp://192.168.1.1:5555");
pb.start();
Try using Runtime.getRuntime().exec() and doing like this :-
String[] command = {"ffplay.exe", "tcp://192.168.1.1:5555"}; // add in String array in sequence, the commands to execute
Process process = Runtime.getRuntime().exec(String[] options, null, new File("C:\\ffmpeg\\bin"));
int returnVal = process.waitFor(); // should return 0 for correct execution
try {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line); //check for the inputstream & see the output here
}
reader.close();
} catch (final Exception e) {
e.printStackTrace();
}
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);
}
}