且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Tomcat中执行shell命令

更新时间:2023-11-30 10:44:46

使用单字符串形式 Runtime.exec 。一个更好的选择是使用 ProcessBuilder ,并且自己拆分参数,而不是依靠Java来为你拆分它(它非常天真)。

It's generally a bad idea to use the single-string form of Runtime.exec. A better option is to use ProcessBuilder, and split up the arguments yourself rather than relying on Java to split them for you (which it does very naïvely).

ProcessBuilder pb = new ProcessBuilder("/bin/mount", "-o", "loop", /*...*/);
pb.redirectErrorStream(true); // equivalent of 2>&1
Process p = pb.start();

你说你在RHEL,所以你有selinux活跃吗?检查你的日志,看看这是阻碍你(我认为它是audit.log你正在寻找,这是从我使用selinux几年)。如果这确实是问题,那么你应该问超级用户或serverfault而不是SO ...

You say you're on RHEL so do you have selinux active? Check your logs and see if this is what's blocking you (I think it's audit.log you're looking for, it's been a few years since I've used selinux). If this does turn out to be the problem then you should probably ask on superuser or serverfault rather than SO...