0

I was hoping someone will be able to give me some advice. I am running a program in java and need to include a function that has already been written in bash. I honestly am not sure where to begin.

I have file that I process to some extent with my java program (I take out duplicates, sort names, etc). I end up with several paths to other files.

for(String go:parsun){
    pathed="/home/S/Data/"+go+"/Ana/lysis/"+go+"/file.vcf";
    System.out.println("Pathed: " + pathed);
 }

I need to take these paths and put them through a bash program which takes the contents of the files and does some math-ish things to their innards. I have seen around the internet that you could do something like this to run a bash script in java:

Process pro=Runtime.getRuntime().exec("shellfilepath");
BufferedReader read=new BufferedReader(new InputStreamReader(pro.getInputStream());

But I am not sure I understand how that works, or how I would go about passing my file paths to it. Also, another consideration is that I will have to slightly modify the bash script to allow it to take in these paths. Previously the paths were hardcoded before hand. I will also need some suggestions on that front.

Is this the best way to have bash and Java work together? Is there another technique? If not, could someone help me with understanding how to use Runtime?

Thanks

Stephopolis
  • 1,765
  • 9
  • 36
  • 65

4 Answers4

2

You could use:

Process process = Runtime.getRuntime().exec("bash_command " + pathed);

The pathed filename will be passed in as the $1 argument.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • So you are saying to use this line and then go through the buffered reader steps? Also, I don't think I made this clear (so sorry) but I will have to alter the bash code to allow it to accept the path (previously the paths were hard coded) do you have any recommendations on that front? – Stephopolis Sep 13 '12 at 17:30
  • Yes, this line should be used within your loop & include the `BufferedReader` part. There is no problem to pass in the full path & use further in your script. – Reimeus Sep 13 '12 at 17:35
1

The following looks like a pretty good intro into running shell scripts from java: http://obscuredclarity.blogspot.com/2011/07/executing-shell-script-from-java.html

It shows how to modify your shell script to accept arguments, and how to use Runtime.exec() to call the script and pass arguments to it.

Also see the API for the Runtime class: http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

Mike Nadaud
  • 106
  • 3
0

Runtime.exec(String) is a good way to do this, but you need to be careful about whether or not the path to your script contains spaces. A better approach would be to use Runtime.exec(String[]). This will make sure that Java treats your script path as one command parameter instead of trying to split it on spaces.

Process process = Runtime.exec(new String[] {shellFilePath});

Also, make sure to include #!/bin/bash at the top of your script.

Brian
  • 17,079
  • 6
  • 43
  • 66
0

You have two choices:

  1. Rewrite the logic from the bash script in Java. This will execute faster and be less brittle, but if the bash script is large and complex or legacy code that nobody left understands, it may be more work than you want to do.
  2. Execute the bash script using ProcessBuilder; you can pass command-line arguments to it. This comes with its own set of issues having to do with coordinating between two processes. If the bash script does not expect any input things are a little simpler, but in general you need to start the script, run input and output handlers on separate threads, and then wait for termination. This is a little difficult to get right if you've never done it, although there are good examples on the web.
Community
  • 1
  • 1
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190