电脑运行java文件路径错误怎么办,java.io.IOException:无法运行程序错误= 2,没有这样的文件或目录...

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("");

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?

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值