java.io.IOException: Cannot run program error=2, No such file or directory
Asked Answered
S

2

9

I have a java class in which I call a runshellscript method that will execute a script. It worked well with mysql but I cannot seem to find out why it wont work well with psql. Here is an excerpt of my runshell method:

public class RunShellScript {

public static void runShellScript (String unixCommand) 
{
 try {
     Runtime runtime=Runtime.getRuntime();
     //Process process=runtime.exec(new String [] { "/bin/csh", "-c", unixCommand});
     Process process=runtime.exec(new String [] {unixCommand});
     InputStream stderr=process.getErrorStream();
     InputStreamReader isr=new InputStreamReader (stderr);
     BufferedReader br=new BufferedReader (isr);
     String line=null;
     System.out.println("<ERROR>");

     while((line=br.readLine())!=null)
         System.out.println(line);

     System.out.println(line);
     int exitVal=process.waitFor();
     System.out.println("Process exitValue:" + exitVal);
 }
 catch (Throwable t)
 {
     t.printStackTrace();
 }

the problem is that when i put this behind a mouse clicked event it says command not found. Here is the code beuind the mous event

private void jMenuItem13MousePressed(java.awt.event.MouseEvent evt)    {                                         

    String shellCommand="vobs/tools/Scripts/DataValidation/mysqlconnection.csh";
    RunShellScript.runShellScript(shellCommand);
    // TODO add your handling code here:
}                     

The weird thing is that when I go directly to the directory where the script resides and type ./mysqlconnection the script works. But when i just type mysqlconnection is says command not found. Somehow it is not recognizing my script name as a command?

Salvidor answered 2/8, 2012 at 23:56 Comment(0)
C
4

Looks like it is similar to the problem I faced, when invoking a shell script (contains system & user created commands) from autosys [autosys -> shell -> Java -> ProcessBuilder]
ProcessBuilder will from a command and execute in Linux machine.
This worked when i log in to Linux box and execute the script but it didn't work when i invoke from autosys.
The actual problem is $PATH variable, which is not being set with the directory of the user created command.
I echo the $PATH variable when executing from Linux machine and Autosys in the shell script, $PATH variable is not set properly when executing from Autosys , after appending user command path to the $PATH variable it worked.
which (cmd) will return the directory of the command, append this directory with $PATH then it will work.

Try adding your script path to $PATH and execute from your application

Crenulation answered 24/3, 2015 at 12:53 Comment(0)
B
0

On unix-like systems, the shell only executes programs residing in the current directory if given an unambiguous path to it. This is to prevent an attacker from, say, putting a program named ls in your home directory which would execute instead of the actual ls program residing in /bin/ls. Thus, the current directory is excluded from the PATH.

Also, try moving

int exitVal=process.waitFor();

to above the while loop.

Bendy answered 3/8, 2012 at 0:3 Comment(37)
so should i add the path to where my csh script is to the path variableSalvidor
You should use ./mysqlconnection in your code instead of mysqlconnectionBendy
i tried...but it doesnt work. So you suggest the path that i assign my string variable to be should be vobs/tools/Scripts/DataValidation/.mysqlconnection.csh";Salvidor
What happens when you run csh -c vobs/tools/Scripts/DataValidation/mysqlconnection.csh?Bendy
it did not work...it still give me the error command not foundSalvidor
you mean assign my sting the that value?Salvidor
you mean run that directly from the terminal?Salvidor
i dont think i should spceicy csh -c because that is already specified inthe string that i pass to runtime.execSalvidor
@Salvidor Yes, I mean run it from the terminal.Bendy
@Salvidor Did you try using ~/vobs/tools/Scripts/DataValidation/mysqlconnection.csh instead of vobs/tools/Scripts/DataValidation/mysqlconnection.cshBendy
hey i tried using the ~ infront and it gives me an error unknown user vobsSalvidor
Are you sure you used ~/vobs and not ~vobs?Bendy
Also, one obvious thing, the file is named mysqlconnection.csh and not mysqlconnection.<something-else>, right?Bendy
yes that is ovbious it is mysqlconnection.csh and it didnt specify it as mysqlconnection.<something-else> so i dont think that is the problem. I did use ~vobs and it says unknown user vobsSalvidor
i even tried to put /vobs...... but it just freezes up completely and i have to shut down netbeansSalvidor
You need to use ~/vobs With both the ~ AND the /Bendy
What is your username? It really, really shouldn't say that. What OS are you running?Bendy
/bin/csh -c vobs/tools/Scripts/DataValidation/mysqlconnection.csh i even tried that directly at the xterm and it works...but my java class is not executing the same stringSalvidor
... Are you absolutely, completely sure that you are using both the tilde and the slash? Like, ~/vobs????Bendy
i am running UNIX and my user name is pagola. if i put the ~ it means home directory and the script doesnt reside there...so i copied the files to my home directory and it still didnt work. I am very very very very positive i am using the tilde and the slash ....it still says unknown userSalvidor
Where does the script reside then? Did you try using the Full Path to the script?Bendy
yes it shows in my above code that the script resides in vobs/tools/Scripts/DataValidation it doesnt make sense to meSalvidor
What is the full path though? If you go into the directory where mysqlconnection.c is and run pwd, what happens?Bendy
the full path is vobs/tools/Scripts/DataValidationSalvidor
Are you sure you ran pwd? The full path should start with a /Bendy
So the full path is /vobs/tools/Scripts/DataValidation?Bendy
yup...this is very irritatingSalvidor
when i put /vobs/tools/Scripts/DataValidation is just hangs up and says <ERROR> but nothing else. The whole systems freezes and i have to do a complete shut downSalvidor
@Salvidor Okay, use the full path (with the /) and maybe try using the suggestion I just made in my edit?Bendy
what are you saying the edit you made...which edit?....i HAVE been using the full path with (/) and it just freezes up on meSalvidor
@Salvidor The edit I made to my question; the suggestion to move a line above the while loop.Bendy
NOPE....it still freezes up .....i dont think it even gets to that point...its gets to the <ERROR>Salvidor
Does the script continue forever maybe?Bendy
i am starting to question the script...but i have run the script from the xterm and it outputs the result to the xterm and writes it to a log file just fine.....but when i try to use java it hangs up...i dont think the script has a problemSalvidor
any new ideas..everything works from command line so i think its the java runtime .exec processSalvidor
i got it to work by only leaving out the whole path but putting ~/mysqlconnection now at least it is reading the fileSalvidor
@Salvidor Congrats! Sorry I couldn't be of more help; it's nice to have the on-site insight. Perhaps you should add your solution as an answer and accept it, so that others will see that this problem is solved.Bendy

© 2022 - 2024 — McMap. All rights reserved.