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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

37 Comments

so should i add the path to where my csh script is to the path variable
You should use ./mysqlconnection in your code instead of mysqlconnection
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";
What happens when you run csh -c vobs/tools/Scripts/DataValidation/mysqlconnection.csh?
it did not work...it still give me the error command not found
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.