Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从shell包含sshpass的Java调用shell脚本_Java_Shell_Unix_Weblogic12c - Fatal编程技术网

无法从shell包含sshpass的Java调用shell脚本

无法从shell包含sshpass的Java调用shell脚本,java,shell,unix,weblogic12c,Java,Shell,Unix,Weblogic12c,我们有一个用例可以将>4GB的数据文件从sftp站点传输到服务器盒。我们已经决定创建一个java服务,它将调用一个shell脚本,实习生可以将该脚本输入到sftp站点和路径,并将文件发送到目标目的地 我们已经编写了以下代码来从Java文件调用shell脚本 processproc=null; String命令=“/mnt/hmdata/loadTest.sh”; System.out.println(“传递命令::”+命令); 试一试{ Runtime rt=Runtime.getRuntime

我们有一个用例可以将>4GB的数据文件从sftp站点传输到服务器盒。我们已经决定创建一个java服务,它将调用一个shell脚本,实习生可以将该脚本输入到sftp站点和路径,并将文件发送到目标目的地

我们已经编写了以下代码来从Java文件调用shell脚本

processproc=null;
String命令=“/mnt/hmdata/loadTest.sh”;
System.out.println(“传递命令::”+命令);
试一试{
Runtime rt=Runtime.getRuntime();
proc=rt.exec(command.trim());
boolean status=proc.waitFor`enter code here`(45,TimeUnit.SECONDS);
log(Level.INFO,“Shell调用成功”);
如果(状态){
msg=“Shell调用成功”;
}否则{
msg=“调用Shell时出错”;
}
}捕获(IOE){
e、 printStackTrace();
}捕获(NullPointerException e){
e、 printStackTrace();
}捕获(例外情况除外){
例如printStackTrace();
}最后{
proc.destroy();
}
shell脚本是这样的

sshpass-p''scp-r@:/finint/inbound/financial/tenv/edw_osccl/*。
一旦调用java服务,我们的服务就无法启动shell。相反,同一个shell在服务器命令提示符下工作正常。我们正在Unix上运行我们的程序


你能推荐一个解决方案吗?

我认为命令应该是
sh/mnt/hmdata/loadTest.sh
而不是
/mnt/hmdata/loadTest.sh
,这样你才能从java进程中使用它。 它可以表示为
String[]
,如下所示:

String[] cmd = { "sh", "/mnt/hmdata/loadTest.sh"};
publicprocessexec(String命令)
publicprocessexec(String命令,String[]envp,File dir)
的便捷方法,而
publicprocessexec(String[]cmdarray,String[]envp,File dir)
,根据

如果您也可以共享
catch
块,这样我们就有了完整的
try catch
块,那就太好了。

。 Java代码没有变化

   Changed Shell Script as
   echo "Hello" >> /mnt/hmdata/output.txt
   chmod 777 /mnt/hmdata/output.txt
   export PASSWD="password"

   sshpass -f /mnt/hmdata/password scp -v -r <user><host>:<path>/<file>
将Shell脚本更改为
echo“Hello”>>/mnt/hmdata/output.txt
chmod 777/mnt/hmdata/output.txt
export PASSWD=“password”
sshpass-f/mnt/hmdata/password-scp-v-r:/

shell仅包含以下命令sshpass-p''scp-r@:/finint/inbound/financial/tenv/edw_osccl/*。上述代码是在独立jar中编写的吗?你有例外吗?您在msg变量中得到的数据是什么?我们已将其公开为REST web服务。Java部分存在于部署在weblogic 12.2.1.1中的ear文件中,我们在调用shell时没有得到任何异常。等待45秒后,状态为布尔值“False”。试着看看上面的问题和代码,使用javacatch(IOException e){e.printStackTrace();}catch(NullPointerException e){e.printStackTrace();}catch(Exception ex){ex.printStackTrace();}最后{proc.destroy();}String[]cmd={sh',“/mnt/hmdata/loadTest sh};-->不工作运行java服务的用户是否具有执行脚本的权限?是,它具有777权限