I have a shell script in a different directory than my java files. This script contains only ls which prints the files in the current directory. When I run the java project it prints the files in the root of the java project not the root of shell script. I want it to print the files in the root of the shell script.
Java code:
ProcessBuilder pb = new ProcessBuilder("/home/omar/ros_ws/baxter3.sh");
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
You then should set the working directory of your ProcessBuilder instance to the directory you want to watch. You can do is using its method directory(File directory).
See Javadoc of ProcessBuilder
so before pb.start(), define a File for your Directory and assign it to the instance of ProcessBuilder.
File myDir = new File("/home/omar/ros_ws");
pb.directory(myDir);
pb.start();
Use the ProcessBuilder.directory, you probably want
String path = "/home/omar/ros_ws/baxter3.sh";
ProcessBuilder pb = new ProcessBuilder(path);
pb.directory(new File(path).getParent());
A simple solution: The command ls can be run with multiple arguments. One of which is a file argument which can be a directory.
From the ls man page:
# List the contents of your home directory
$ ls ~
Thus, just pass your shell script the directory you have hard coded in your java code: "/home/omar/ros_ws/" and access it in your shell script via $1.
So your shell script will look something like:
#!/bin/bash
ls $1
And call your shell script from java via:
ProcessBuilder pb = new ProcessBuilder("/home/omar/ros_ws/baxter3.sh /home/omar/ros_ws/");
// other code omitted for brevity
Related
Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt");
I am running this command using Java. The script is running but it's not redirecting its stream to the file. Moreover, the file out.txt is not getting created.
This script runs fine if I run it on shell.
Any ideas?
You need to use ProcessBuilder to redirect.
ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh");
builder.redirectOutput(new File("out.txt"));
builder.redirectError(new File("out.txt"));
Process p = builder.start(); // may throw IOException
When you run a command, there is no shell running and any shell commands or functions are not available. To use something like &> you need a shell. You have one but you are not passing it to it. try instead.
Runtime.getRuntime().exec(new String[] { "sh", "somescript.sh &> out.txt" });
I've got a python file which opens a webpage with selenium.How can I run this file in java application ? I tried Runtime().exec("python main.py") and "python3 main.py" but it did't work.
Use ProcessBuilder.
String[] commands = { "python", "main.py" }; // Need to add a python or python3 to the system path
ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory("Set the path of the main.py folder");
Process proc = pb.start();
I am trying to execute "python3 --version" (this is just an example) from Java using ProcessBuilder. python3 is located in /usr/local/bin. I have configured the working directory. Here is my code snippet :
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "python3 --version");
pb.directory(new File("/usr/local/bin"));
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
reader.close();
But it gives the error : /bin/bash: python3: command not found. Any way to resolve this?
PS : It can execute python --version as it is located in /usr/bin. Rather it executes successfully all commands pertaining to /usr/bin but none of the ones located in /usr/local/bin. python3 is just an instance of the general problem I am facing.
We also have to configure the environment (more so the PATH variable) and append /usr/local/bin as well to it. It will work fine then. I use the Eclipse IDE and I configured the PATH under environment in Run Configurations. It works fine now.
Im actually doing a Java Program that execute a shell script with this ProccesBuilder:
ProcessBuilder builder = newProcessBuilder("sh","script.sh",params);
builder.redirectOutput(new File("outNormal.txt"));
builder.redirectError(new File("outError.txt"));
Process p = builder.start();
p.waitFor();
The problem i have is that outNormal.txt and outError.txt, and another file my script creates are placed at the same level as /src and /bin, and i want to change this to a new directory.
is this possible?
Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt");
I am running this command using Java. The script is running but it's not redirecting its stream to the file. Moreover, the file out.txt is not getting created.
This script runs fine if I run it on shell.
Any ideas?
You need to use ProcessBuilder to redirect.
ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh");
builder.redirectOutput(new File("out.txt"));
builder.redirectError(new File("out.txt"));
Process p = builder.start(); // may throw IOException
When you run a command, there is no shell running and any shell commands or functions are not available. To use something like &> you need a shell. You have one but you are not passing it to it. try instead.
Runtime.getRuntime().exec(new String[] { "sh", "somescript.sh &> out.txt" });