Executing Nginx from java application
I'm trying to start nginx HTTP server from java application using
Runtime.getRuntime().exec(command);
the command variable contents is :
/media/data/websites/php-desktop/bin/nginx/nginx/nginx -c "/tmp/conf/nginx.conf" -p "/media/data/websites/php-desktop/bin/nginx/"
the command should start nginx from teh path specified, with the config file after -c and with the prefix value after -p
the problem is java doesn't start nginx 开发者_如何学Pythonit just falls silently, i tried to print the procccess output put it doesn't output anything.
beside when i execute the command from any terminal it works fine. Note: i use ubuntu linux, nginx built from source with latest stable version.
Because
Runtime.getRuntime().exec(command);
is equivalent to
Runtime.getRuntime().exec(command, null, null);
So it won't work because the array of arguments is null and exetable file is "/media/data/websites/php-desktop/bin/nginx/nginx/nginx -c /tmp/conf/nginx.conf -p .... ".
Please try either of
Runtime.getRuntime().exec(new String[] {
"/bin/sh"
,"-c"
,"/media/data/websites/php-desktop/bin/nginx/nginx/nginx -c \"/tmp/conf/nginx.conf\" -p \"/media/data/websites/php-desktop/bin/nginx/\"});
Or
Runtime.getRuntime().exec(new String[] {
"/media/data/websites/php-desktop/bin/nginx/nginx/nginx",
"-c",
"/tmp/conf/nginx.conf",
"-p",
"/media/data/websites/php-desktop/bin/nginx/"});
Try reading off the process's inputStream. Something like this ...
Process p = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
Edit: also try the errorStream instead ... http://download.oracle.com/javase/6/docs/api/java/lang/Process.html#getErrorStream%28%29 (that's probably where you'll find any errors!)
HTH
精彩评论