rana123
rana123

Reputation: 159

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 workaround for this?

Thank you, Rana.

Upvotes: 0

Views: 2771

Answers (2)

AlexR
AlexR

Reputation: 115328

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.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328574

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.

Upvotes: 2

Related Questions