java.io.IOException:无法运行程序错误= 2,没有这样的文件或目录
我有一个Java类,其中调用了将执行脚本的runshellscript方法。它在mysql上运行良好,但我似乎无法找出为什么在psql下无法正常运行。这是我的runshell方法的摘录:
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:
}
奇怪的事情是当我直接转到脚本所在的目录并键入./mysqlconnection时,脚本可以工作。但是,当我只键入mysqlconnection时,说找不到命令。
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?
看起来有点像我在调用时遇到的问题来自autosys的Shell脚本(包含系统和用户创建的命令)[autosys-> shell-> Java-> ProcessBuilder] ProcessBuilder
命令并在Linux机器中执行。
当我登录到Linux框并执行脚本时,此方法有效,但是当我从autosys调用时,该方法不起作用。
实际问题是 $ PATH
变量,未在用户创建的命令的目录中设置。
从Linux计算机上执行时,我回显$ PATH变量,并且在外壳程序脚本中的Autosys中,从Autosys执行时,在将用户命令路径附加到$ PATH变量后可以正常工作时,无法正确设置$ PATH变量。其中(cmd)
将返回命令的目录,并在此目录后附加$ PATH即可使用。
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.
尝试将脚本路径添加到$ PATH并从应用程序执行
Try adding your script path to $PATH and execute from your application