Issue
I am executing a command in my doPost()
method of Servlet Class
which runs on Tomcat.
The command is :
plink -pw **** username@hostname tail -1000 /temp/info.txt
The command runs fine when I run it on my windows console but when executed inside servlet it throws this error.
java.io.IOException: Cannot run program "plink": CreateProcess error=2, The system cannot find the file specified
I tried executing the command through Runtime.getRuntime().exec(command)
as well as through
ProcessBuilder pb = new ProcessBuilder("plink", "-pw", "***","username@hostname","tail","-1000","/temp/info.txt")
Solution
A user's environment is typically not in effect when a program is run via certain intermediary actors since a shell's PATH is set in .bashrc, to be executed anew with each new shell.
So, a Java process, for instance, doesn't even run a shell, hence a search in PATH's directories isn't made.
Use full pathnames for programs run via Process on this and via remote execution services on another system.
Answered By - laune
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)