来自tomcat6的Runtime.exec()成功,但无法访问任何文件 [英] Runtime.exec() from tomcat6 succeeds, but cannot access any files
问题描述
Runtime.exec()
执行命令的问题。 (命令应该是生产中的ImageMagick的
convert
,但是我已经简化了调用来找到问题的根源,所以所有以下代码与 echo
被测试,并且不工作)。 String command = echo test123> / tmp / tomcat6-tmp / 1;
运行时runtime = Runtime.getRuntime();
进程进程= runtime.exec(command);
int exitVal = process.waitFor();
似乎是很常见的调用外部程序的方式。它运行,在 exitVal
中返回 0
,但无法创建文件并将文本放在其中。
那么低层次的方法是:
ProcessBuilder pb = new ProcessBuilder(echo,test123> / tmp / tomcat6-tmp / 3);
进程进程= pb.start();
int resInt = process.waitFor();
但是可以创建一个文件并在其中放入一些文本使用以相同方法放置的Java代码:
String fname =/ tmp / tomcat6-tmp / 2;
文件文件=新文件(fname);
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(test123);
fileWriter.close();
Runtime.exec(whoami)
成功返回 tomcat6
,文件夹 / tmp / tomcat6-tmp /
确实存在,所有权限设置正确。 p>
$ ls -al / tmp / tomcat6-tmp /
共60
drwxr-xr-x 2 tomcat6根4096 3月2日15:26。
drwxrwxrwt 6根根4096 3月2日15:25 ..
-rw-r - r-- 1 tomcat6 tomcat6 7 3月2日15:26 2
所有不需要访问系统文件的命令都可以正常执行, Runtime.exec()
在相同的上下文中。
我使用从包中安装的tomcat6全新安装debian挤压,在配置中没有任何修改:
$ aptitude show tomcat6
包装:tomcat6
状态:已安装
版本:6.0.28-9 + squeeze1
.....
$ cat / etc / issue
Debian GNU / Linux 6.0 \\\
\l
如何解决问题?至少我应该在哪里看?我已经google了各种可能的Java的原因,以这种方式行事,但没有找到线索。
PS 由于这是默认安装, Java安全管理器在 /etc/init.d/tomcat6中禁用
#使用Java安全管理器? (是/否)
TOMCAT6_SECURITY =否
将您想要的操作放入单个可执行的shell脚本中,然后将 exec
shell脚本。
Java的 Runtime.exec()
是围绕 exec
系统调用的包装器,它将直接运行该过程,而不是在子-贝壳。 >
重定向由shell执行,不会直接作为参数直接执行 exec
ed process 。
I'm moving our servlets (pure Java, running in Tomcat 6) from CentOS to Debian, and faced the problem with executing commands with Runtime.exec()
.
(The command should be ImageMagick's convert
in production, but I have simplified the calls to find the source of problems, so all the following code with echo
is tested and not working as well).
String command = "echo test123 > /tmp/tomcat6-tmp/1";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
int exitVal = process.waitFor();
Seems to be pretty common way to call an external program. It does run, returns 0
in exitVal
, but fails to create file and put text in it.
So does low-level approach:
ProcessBuilder pb = new ProcessBuilder("echo", "test123 > /tmp/tomcat6-tmp/3");
Process process = pb.start();
int resInt = process.waitFor();
But it is possible to create a file and put some text in it using Java code placed in the same method:
String fname = "/tmp/tomcat6-tmp/2";
File file = new File(fname);
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("test123");
fileWriter.close();
Runtime.exec("whoami")
successfully returns tomcat6
, the folder /tmp/tomcat6-tmp/
does exist, all permissions are set correctly.
$ ls -al /tmp/tomcat6-tmp/
total 60
drwxr-xr-x 2 tomcat6 root 4096 Mar 2 15:26 .
drwxrwxrwt 6 root root 4096 Mar 2 15:25 ..
-rw-r--r-- 1 tomcat6 tomcat6 7 Mar 2 15:26 2
All commands without need to access files in system are seem to execute normally with Runtime.exec()
in the same context.
I use fresh install of debian squeeze with tomcat6 installed from packages, without any modifications in configuration:
$ aptitude show tomcat6
Package: tomcat6
State: installed
Version: 6.0.28-9+squeeze1
.....
$ cat /etc/issue
Debian GNU/Linux 6.0 \n \l
How can I solve the issue? Or at least where should I look? I've googled every imaginable reason for Java to misbehave this way, but failed to find a clue.
P.S. As this is default installation, Java security manager is disabled in /etc/init.d/tomcat6
# Use the Java security manager? (yes/no)
TOMCAT6_SECURITY=no
Put the action you want into a single executable shell script, then exec
the shell script.
Java's Runtime.exec()
is a wrapper around the exec
system call, which will run the process directly, rather than under a sub-shell. The >
redirection is carried out by the shell, and will not work as an argument to a directly exec
ed process.
这篇关于来自tomcat6的Runtime.exec()成功,但无法访问任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!