Using ProcessBuilder to execute commands that require comand line input - java

I was knocking up a app to try and help with my build process and was trying in include a git pull origin command. I've tried and tested all kinds of methods, but can't seem to get any input or output to work with that command.
The program waits on Process.waitFor() with no input or output.
I'm assuming the git pull command is waiting for me to input a username and password, but so far I've failed to establish how to achieve that.
There are numerous questions on SO coverings these kinds of examples, but none of the answers appear to be working for my use case.
The simplest code that I believe should work is:
class CmdTest {
public static void main(String[] args) throws Exception {
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "git pull origin");
pb.redirectErrorStream(true);
pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process process = pb.start();
process.waitFor();
}
}
Replacing the git pull origin with a dir will provide output as expected.

Doing just git pull origin without the credentials configured by default, git will wait for them at any important action. In your code, you don't provide (neither look) for git feedback, so the application get stuck at this stage.
Configuring the default credentials for git into your .gitconfig file, any git command will use them and the console won't be stuck (neither your application).

Related

Restart Tomcat with Java

I need to restart the tomcat from Java code.
For eg, if a query is not executed for a certain time Period then it will restart the tomcat automatically.
I have tried the following shutdown and startup code, but when we shutdown the tomcat then the java code will not run and tomcat not started.
Note :- I am running this code from a application and restarting the same tomact which the same application is using.
Following the code
try {
PreparedStatement.setQueryTimeout(10);
rs = PreparedStatement.executeQuery();
} catch (SQLException ex) {
System.out.println("IN CATCH BLOCK FOR THE REFRESH INVOICE");
String shutcommand = "killall java";
Process shutchild = Runtime.getRuntime().exec(shutcommand);
System.out.println("JAVA PROCESS KILLED");
String locationCommand = "cd /root/cluster/tomcat6/bin";
Process locationChild = Runtime.getRuntime().exec(locationCommand);
String strtcommand = "./startup.sh";
Process strtchild = Runtime.getRuntime().exec(strtcommand);
}
Killing tomcat after SQLException this is not good idea to handle this exception. Probably the problem is on database site.
But if you are sure, that it is what you need you can kill this java proces in this section, but to run tomcat you should use for example bash and cron. Why? Beacues after killing your executing code will stop, so you don't achieve the line to start tomcat.
How to check tomcat: Is Tomcat running?
TL;DR
File binaryDir = new File(System.getProperty("catalina.home") + File.separator + "bin");
String restartCommand = "\"shutdown.bat & ping 0.0.0.0 -n 4 & C:\\WINDOWS\\system32\\net start Tomcat8\"";
new ProcessBuilder("cmd", "/c", restartCommand).directory(binaryDir).start();
Survive
Creating new process will survive JVM shutdown. If you combine commands in one line
it should work fine according to my tests and this.
shutdown.bat
You need to use shutdown.bat instead stopping windows service because it often fails on Windows with message Cannot stop service Apache Tomcat...
ping 0.0.0.0 -n 4
You need to wait some time after shutdown otherwise you will get Service is already starting. Try again later error message. Also note I use ping instead timeout because it causes problems on some systems.
C:\WINDOWS\system32\net start Tomcat8
I'm starting windows service because invoking startup.bat won't work for me. Also remember to replace Tomcat8 if you using different tomcat or custom service name for example Tomcat7
Redirect I/O
Don't redirect input or output of process instance or command will shutdown with JVM, and Tomcat won't start.
You can execute this native command using java
String command = "c:\program files\tomcat\bin\startup.bat";//for linux use .sh
Process child = Runtime.getRuntime().exec(command);
You should consider using ProcessBuilder instead of Runtime exec. Also, you should split all the arguments when you want to execute a command.
I suggest this :
ProcessBuilder shutcommand = new ProcessBuilder("killall", "java");
Process shutchild = shutcommand.start();
System.out.println("JAVA PROCESS KILLED");
ProcessBuilder strtcommand = new ProcessBuilder("/root/cluster/tomcat6/bin/startup.sh", "java");
Process strtchild = strtcommand.start();

Running shell script on tomcat7

I have been breaking my head for two days trying to fix the file permissions for my tomcat7 server. I have a library class (.jar file included in myapp/WEB-INF) which needs to run a shell script. The library is written by me and works fine within NetBeans ie. no hassle in creating,reading and deleting files. That is because NetBeans runs the program as blumonkey(my username on my Ubuntu System). But when I import this into tomcat and run it, tomcat "executes" the command, produces no definite output, tries to check for a file(which will be generated when the script succeeds) and throws a FileNotFoundException.
More Details as follows:
Tomcat7 installed using apt-get, has its data in 2 locations - /var/lib/tomcat7 with conf and webapps folders and /usr/share/tomcat7 with the bin and lib folders
The user uploads a .zip file which is stores to /home/blumonkey/data. Rest of the program runs on the documents stored here. All new folders/files uploaded by tomcat have, obviously, tomcat7 as the owner.
I have tried things like changing the ownership to blumonkey, adding tomcat7 to blumonkey user group but none of the methods worked (Somewhere around here I probably messed up changing permissions carelessly :/ ). Apparently tomcat7 is unable to process on the files it owns.(How can this be?).
The script works when I run it in the terminal. But it doesn't work when I do a sudo -u tomcat7 script.sh, ie run it as tomcat7. It just exits with no message. I doubt that this it what is happening as I have tried to debug by redirecting the errors and outputs in ProcessBuilder but they came empty.
Any help regarding how to fix the issue and get the script running would be greatly appreciated. Please comment if you need any more info.
The code for script execution
private static void RunShellCommandFromJava(String command,String fn, String arg1,String arg2) throws Exception
{
try
{
System.out.println(System.getProperty("user.name"));
ProcessBuilder pbuilder = new ProcessBuilder("/bin/bash",command,fn,arg1,arg2);
System.out.println(pbuilder.command());
pbuilder.redirectErrorStream(true);
Process p = pbuilder.start();
p.waitFor();
}
catch(Exception ie)
{
throw ie;
}
}
The command which needs to be executed
"/bin/bash /abs/path/to/script.sh /abs/path/to/doc/in/data-folder maxpages=30 maxsearches=3"
PS : I have followed this question but it didn't help. I also tried other options like Runtime.exec(), bash,/bin/bash/ and /bin/bash/ -c, some of them don't work at all, others give no results.
Try to use Runtime and check standard error to find out what was the problem (probably permissions or paths):
// run command
String[] fixCmd = new String[] { "/bin/bash", "/abs/path/to/script.sh", "/abs/path/to/doc/in/data-folder", "maxpages=30", "maxsearches=3" };
Process start = Runtime.getRuntime().exec(fixCmd);
// monitor standard error to find out what's wrong
BufferedReader r = new BufferedReader(new InputStreamReader(start.getErrorStream()));
String line = null;
while ((line = r.readLine()) != null) {
System.out.println(line);
}

How to Terminate a Process Normally Created using ProcessBuilder

I am creating Processes using ProcessBuilder in my Java Application. The created process executes some FFMPEG commands which actually copy the RTSP streams in specified destination media file.
ProcessBuilder builder = new ProcessBuilder("ffmpeg", "-i", RTSP_URL, "-f", fileFormat, destFilePath);
Process processToExecute = builder.start();
I want to close the process before it completes its execution. So, If I run this FFMPEG command directly in windows CMD and then press 'CTRL+C' after 5 seconds then process get terminates with status '2'. And I can play the media file created so far.
So, If I do the same operation in my Java Application using:
process.destroy(); //I call this method after 5 sec
I get the status code '1' which means abnormal termination. I get the status by the following way:
processToExecute.destroy();
processToExecute.exitValue(); //This return me status '1'
And I can't play the media file and I think this is due to the abnormal termination of the process.
So how I can terminate the process created using ProcessBuilder in the same way we do in CMD with (CTRL+C) so that I may play the created media file ?
I want to terminate process (created using ProcessBuilder) in Java Application with status code of '2' that I get when I terminate process using CMD.
EDIT#01: --- Sharing Findings
So, when I try to delete that file once app terminates, I get the following error:
The Action Can't be Performed Because File is Opened in FFMPEG.exe
Which means that process is not terminating the command it is executing. That command still has occupied this file that's why I am not getting able to play it. Process gets terminate when I call:
processToExecute.destroy();
But, the task it is performing (that is execution of a command) is still active. Strange!!!!
EDIT#02: Sharing Ultimate Reason
Actually If I directly press 'CTRL+C' or 'q' in cmd when process is running then it terminates the process successfully and this process is no more visible in the currently executing processes lists.
And Programatically when I call method:
cmd> processToExecute.destroy();
It terminates the process but when I see the list of currently executing processes I can still see them over there.
And same scenario exists If I try to terminate this process using 'taskkill' or 'kill' command in another CMD by specifying their's name or pid that still process terminates abnormally.
P.S. I use the following command to see the running processes:
tasklist
So from this it proves that destroy() method from Application and 'taskkill or kill' command from another CMD is not terminating the process normally that pressing 'CTRL+C' and 'q' does.
Maybe try...
builder.inheritIO();
System.exit(2);
Or you could try to write to the stdin of the process...
process.getInputStream().write(exitCode);

Why am I getting a "Path not correct" error while running a batch file on a remote system?

I need to run a batch file on a remote system from a local machine. Using the code below, I am getting the following error:
Path not correct
I have the IP address of the machine, and I have given the batch file as public share and share name is dsc.
The IP address of the machine is 16.181.37.28.
Here is my code. I know that the path is wrong. How can I give the exact path?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author padmaja
*/
import java.io.*;
class Test{
public static void main(String arg[]){
try{
String command = "cmd /C start 16.181.37.28/dsc/StartVisTsDataCenterMySql-log.bat";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
}catch (IOException e) {
e.printStackTrace();
}
}
}
Are you trying to run the script ON THE REMOTE machine or ON YOUR LOCAL machine? Your approach will read the file from the remote machine but run it on your local.
The usual way to get something running on a remote machine is having a process on the remote machine to run permanently and listen for requests. If a request arrives this process will start your batch file you would like to have run.
You're running on Windows but using Posix paths separators. Try "cmd /C \\\\16.181.37.28\\dsc\\StartVisTsDataCenterMySql-log.bat".
You can test whether it works by creating a service:
sc \\16.181.37.28 create StartVisTsDataCenterMySql-Log binPath= "cmd /c \\16.181.37.28\dsc\StartVisTsDataCenterMySql-log.bat"
Then the command to run it is:
"cmd /c sc \\16.181.37.28 stop StartVisTsDataCenterMySql-Log&sc \\16.181.37.28 start StartVisTsDataCenterMySql-Log"
You'll need to be connected to the share as an administrator (or have the credentials saved). Once you've confirmed that works, change to srvany, as you will get an error in the event log and the batch file will only be allowed to run for 30 seconds otherwise.
If that's not the right answer, perhaps you can elaborate on the real requirement, and give some information on whether reimplementing the batch file in Java is a realistic solution.

IntelliJ Plugin - Run Console Command

I am new to plugin development for IntelliJ and would like to know, how I can execute a command in the command line from within my plugin.
I would like to call, for instance, the command "gulp" in the current projects root directory.
I already tried using
Runtime.getRuntime().exec(commands);
with commands like "cd C:\Users\User\MyProject" and "gulp", but it does not seem to work that way and I wonder, if the plugin API provides an easier method.
I know its a bit late (1 year later), but recently I was working on an IntelliJ plugin and I had the same issue and this is what I used and it works pretty well.
First, we need to create a list of commands that we need to execute:
ArrayList<String> cmds = new ArrayList<>();
cmds.add("./gradlew");
Then
GeneralCommandLine generalCommandLine = new GeneralCommandLine(cmds);
generalCommandLine.setCharset(Charset.forName("UTF-8"));
generalCommandLine.setWorkDirectory(project.getBasePath());
ProcessHandler processHandler = new OSProcessHandler(generalCommandLine);
processHandler.startNotify();
hence the generalCommandLine.setWorkDirectory is set to the project directory which could be equivalent to the terminal command cd path/to/dir/
The Runtime class provides exec(String[], String[], File) method where the last argument is working directory of the subprocess being launched.
The plugin API provides OSProcessHandler class (as well as other classes like ProcessAdapter) which can help to manage the subprocess, handle its output etc.
ProcessOutput result1 = ExecUtil.execAndGetOutput(generalCommandLine);
result1.getStdOut result1.getStdErr works
and
ScriptRunnerUtil.getProcessOutput(generalCommandLine, ScriptRunnerUtil.STDOUT_OUTPUT_KEY_FILTER, timeout);
both work pretty well
and they are built into intellij
import com.intellij.execution.process.ScriptRunnerUtil;
import com.intellij.execution.util.ExecUtil;

Categories