Java.io.IOException: Cannot start program error = 2, No such file or directory

I have a java class in which I call the runhellscript method that will execute the script. It worked fine with mysql, but I can't figure out why it doesn't work with psql. Here is an excerpt from my runhell 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 the event with a click of the mouse, it says that the command was not found. Here is the code for 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 strange thing is that when I go directly to the directory where the script is located and types. / mysqlconnection, the script is running. But when I just type mysqlconnection, it says that the command was not found. For some reason, this does not recognize my script name as a command?

+7
source share
2 answers

It looks like this is the problem I encountered when calling the shell script (contains system and user commands) from autosys [autosys → shell → Java → ProcessBuilder]
ProcessBuilder will be from a command and executed on a Linux machine.
This worked when I enter Linux and run the script, but it does not work when I call from autosys.
The actual problem is the $PATH variable, which is not set in the directory of the user-created command.
I repeat the $ PATH variable when executed from a Linux machine and Autosys in a shell script, the $ PATH variable is not set properly when executed from Autosys after adding the path to the $ PATH command line with which it worked.
which (cmd) will return the command directory, add this directory with $ PATH, then it will work.

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

+2
source

On unix-like systems, the shell only executes programs in the current directory if they are given an unambiguous path to it. This is done so that an attacker, for example, places a program called ls in the home directory that would run instead of the actual ls program located in /bin/ls . Thus, the current directory is excluded from PATH.

Also try moving

 int exitVal=process.waitFor(); 

above the while .

0
source

All Articles