not able to run .sh file using java

2019-08-31 14:49发布

问题:

I have a .sh file which i am trying to run using runtime api. It runs perfectly if .sh file is under main project folder, but I want it to be accessed from outside the project folder.

example: /home/test/test.sh.

When i try to run this I get /home/test/test.sh not found error

Can anyone tell me how to execute .sh file using runtime accessing the file from local system?

回答1:

Runtime.exec() is not a command line One pitfall to cover with Runtime.exec() is mistakenly assuming that exec() accepts any String that your command line (or shell) accepts. Runtime.exec() is much more limited and not cross-platform. This pitfall is caused by users attempting to use the exec() method to accept a single String as a command line would. The confusion may be due to the fact that command is the parameter name for the exec() method. Thus, the programmer incorrectly associates the parameter command with anything that he or she can type on a command line, instead of associating it with a single program and its arguments.



回答2:

is the .sh file chmoded to be executable?

if its not you could either:

chmod +x /home/test/test.sh

or

when you call the script pass it through sh so:

sh /home/test/test.sh


标签: shell