I want to run a .sh file using java. I want a terminal to be opened and then I can execute another commands in the same terminal and finally destroy it.
I already used ProcessBuilder but I could not accomplish this.
My piece of code:
ProcessBuilder pb = new ProcessBuilder("/home/omar/ros_ws/baxter2.sh");
Process p = pb.start();
This method used to work in another code, but I don't know why it's not working in mine.
Thanks in advance
How do you know that it doesn't execute? Maybe you just aren't seeing its result. You should get p.getInputStream() after executing and print in your console, like:
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
Also if you're using jdk 7+, try:
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
Process p = pb.start();
Does your program output an error, or is your program not interacting with the file?
I would suggest trying the directory method within ProcessBuilder.
Process p = null;
ProcessBuilder pb = new ProcessBuilder("baxter2.sh");
pb.directory("/home/omar/ros_ws");
p = pb.start();
If this doesn't work, you should also look into user permissions for the file that you're trying to access.
I think you should grant the .sh file the executable permission to the OS user used to run the java program by using the below command.
chmod u+x baxter2.sh
Related
I'm trying to use Runtime.getRuntime().exec() to call a program as if it was called from the terminal, but it just crashes with a fatal error after reading the first file.
In the terminal I run the command like so:
mace4 -c -f inputFile.in > outputFile.out
It works as expected, reading from the first file and outputting in the second one.
In Java I try to run it this way:
String args[] = new String[]{"mace4", "-c", "-f", inputFileName ,">",outputFileName};
try {
String s;
Process proc = Runtime.getRuntime().exec(args, null, new File("/home/user/workDirectory/"));
BufferedReader br = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
while ((s = br.readLine()) != null)
System.out.println("line: " + s);
proc.waitFor();
proc.destroy();
As soon as the program reaches the end of the first file, it throws this:
Fatal error: read_all_input, file > not found
The program is quite old and I can't seem to find a way to get a more detailed error out of it..
I tried calling it with these arguments {"sh or bash", "-c", "mace4", "-c", "-f", inputFileName ,">",outputFileName} which makes the program run and then freeze (or at least nothing appears in the console)..
Am I calling the terminal command wrong and if yes what should I change?
PS: this is my first question here, if I missed anything, I'm sorry..
It looks like you're trying to use the Bash output redirection operator >. This redirects the output of the program you're running to a file (or another program)
This answer explains how to do this using ProcessBuilder which should work for what you're trying to do here.
For example:
ProcessBuilder pb = new ProcessBuilder("mace4", "-c", "-f", inputFileName);
pb.redirectOutput(new File(outputFileName));
Process p = pb.start();
I am trying to use Java to find out the versions of Java installed on a machine. I have:
List<String> commands = new ArrayList<String>();
commands.add("java.exe");
commands.add("-version");
ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory(new File("C:\\Program Files\\Java\\jdk1.6.0_45\\bin"));
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
However, when this is run, the while loop is never executed as the stdInput is empty. If I take out the commands.add("-version"), it will get the input that is output when running "java.exe" command on command line, so it seems adding the -version arguement is causing issues and this also indicates that the directory and java.exe commands are correct. Any help would be appreciated.
The output of java -version is sent to the error stream - reading from that stream should result in the proper output:
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getErrorStream()));
Alternatively, you can call redirectErrorStream(true) to merge the Input and Error streams. If you just wish to just print to the command line, you can use inheritIO on the ProcessBuilder.
The currently running java version can be found without the need for a ProcessBuilder by retrieving the appropriate System property
System.out.println(System.getProperty("java.version"));
java -version prints to stderr. Try using:
ProcessBuilder pb = new ProcessBuilder(commands).redirectErrorStream(true);
That will put stderr in the same stream as stdout and the rest of your code can look as it does now.
This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 8 years ago.
I have an java application.And I use Runtime.getRuntime().exec for call a batch file.When I call a linux batch file using with Runtime.getRuntime().exec the batch file could not find its own directory.
I use pwd command in batch file but it returns application path.
I need batch file's own physical path from itself.
How can I do this?
You must use a ProcessBuilder in order to acomplish that:
ProcessBuilder builder = new ProcessBuilder( "pathToExecutable");
builder.directory( new File( "..." ).getAbsoluteFile() ); //sets process builder working directory
Try this . Its working for me.
Process p = Runtime.getRuntime().exec("pwd");
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();
Batch files, if you're specifically referring to files with the '.bat' extension, are designed to be used with Microsoft's Command Prompt shell ('cmd.exe') in Windows, as they are script files containing a sequence of commands specifically for this shell, and as such will not work with Unix shells such as Bash.
Assuming you actually mean a Unix 'shell script', and not specifically a Microsoft 'batch file', you'd be better off using the ProcessBuilder class, as it provides greater flexibility than Runtime's exec() method.
To use ProcessBuilder to run a script in its own directory, set the builder's directory to the same directory that you're using to point to the script, like so:
// Point to wherever your script is stored, for example:
String script = "/home/andy/bin/myscript.sh";
String directory = new File(script).getParent();
// Point to the shell that will run the script
String shell = "/bin/bash";
// Create a ProcessBuilder object
ProcessBuilder processBuilder = new ProcessBuilder(shell, script);
// Set the script to run in its own directory
processBuilder.directory(new File(directory));
// Run the script
Process process = processBuilder.start();
I am attempting to run msys.bat in java using ProcessBuilder. When I run the .bat file with my program, the following error occurred: "Cannot find the rxvt.exe or sh.exe binary -- aborting. Press any key to continue . . ."
Here is the code,
ProcessBuilder Msys = new ProcessBuilder("C:/msys/1.0/msys.bat", "/C", "find \"C:/Users/Dan G/Desktop/hello.elf\"");
Process p = Msys.start();
String line;
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = r.readLine()) != null) {
System.out.println(line);
}
r.close();
The goal is to compile some C projects of mine. The command up there is just to test for a result, not what I want to accomplish.
Thanks for the help!
.bat files can't run on their own and are called on the Windows command processor. So don't forget to load the Windows command processor too, cmd.exe before your bat file.
ProcessBuilder Msys = new ProcessBuilder("cmd.exe", "C:/msys/1.0/msys.bat",
"/C", "find \"C:/Users/Dan G/Desktop/hello.elf\"");
Edit
Please check out this useful article for tips and traps that occur with this process: when runtime.exec() won't. The code in the article is a bit dated, but the concepts are just as germane today as they were then. It is highly recommended.
How would I go about executing a .SH file (this is localhost, no remote connection or anything)? I've seen lots of Runtime.exec and other things when I searched but those didn't seem to work.
This is Java 6. Also if it matters, all the SH is doing is moving two folders around.
Thanks!
You could use ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("myshell.sh", "myArg1", "myArg2");
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 may also give some consideration to the JSch library if you do not want to make your code platform-dependent by directly invoking OS commands.