开发者

java - execute a command in linux

I have a servlet running in tomcat6. I use the following code to execute a command in ubuntu Runtime.getRuntime().exec("/usr/bin/wine cmd /c some.vbs"); the problem is tomcat6 runs the programme as "tomcat6" user, the above java command. So the the above commands getErrorStream returns as "wine: /home/randeel/.wine is not owned by you" I have installed "wine" using user "randeel". Is there a 开发者_C百科workaround for this?

Thank you, Rana.


Yes: You must switch the user. Try

Runtime.getRuntime().exec("sudo -U randeel /usr/bin/wine cmd /c some.vbs");

Note that sudo will ask for a password unless you configure it otherwise.

Another, more elegant solution, is to run a little server as randeel which waits for a network connection. It then runs the command and returns the output via the network connection. See the documentation for java.net.Socket.


You have 2 solutions. 1. give appropriate execute permissions to wine using chmod a+x. 2. If you do not want to do #1 you have to run command line that first changes the user and then runs the application. command su USERNAME changes current user but requires typing password. To emulate the terminal that types password you can use expect script.

Then you can run command as different user. If you are going to use this way I'd recommend you to write short shell script that does these 2 actions and run this script from java.

The #2 is more complicated. Way #1 seems to be much simpler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜