Atticus29 Atticus29 - 4 months ago 51
Bash Question

Running a shell script from Java returning "command not found"

I've created a java GUI using NetBeans v.8.2. Very new to Java.

One of the buttons in the GUI launches a shell script (I am aware that this is not ideal Java practice, but it is appropriate for my use case) using arguments gathered from other buttons/text fields in the GUI:

private void RunMacActionPerformed(java.awt.event.ActionEvent evt) {
String command[] = {scriptDirStr + "/./",
scriptDirStr +"/"};
ProcessBuilder pb = new ProcessBuilder(command);
try {
Process p = pb.start();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);

So, the idea is to launch with a bunch of arguments. runs other R and python scripts, e.g.:

python2 $script_Path/ $spath >>$spath/masterOutput.txt 2>>$spath/masterErrors.txt


Rscript $script_Path/1_APS_generator_master.R $spath $dpath $APS_src_filename $project_ID $APS_norm_src_filename >>$spath/masterOutput.txt 2>>$spath/masterErrors.txt

and ends with

cat $spath/masterErrors.txt| mail -s $Project_title" done"

I know the script gets launched because I get an email with the following errors:

"...line 14: python2: command not found"


"...line 16: Rscript: command not found"

When I run with the same exact arguments from within the terminal, there are no such errors. Does anybody know what might be going wrong and/or how to fix it?

To rephrase the problem, it seems I am getting different behavior launching the same commands from within java vs. directly onto the command line.


Your shell environment is clearly different from java's environment. Try specifying the full path to python2 and Rscript. For example

/usr/local/bin/python2 $script_Path/ ...