Capturing output of exe while process is yet not completed - java

I am working with one cmd like executable and source code of that is not available.
Executable is having multiple command sequence to be entered before it exit. Each command takes random time from 1 sec to 1 min.
I tried with Java's Process builder and runtime but I am getting output only after program completely exits from execution. Also, I am not able to insert any command to that executable via BufferedWriter. So my question is how to get output of executable while it is still running
Quenstions referred: Executing an EXE from Java and getting input and output from EXE
java runtime.getruntime() getting output from executing a command line program etc.

Related

How to Make Java Open a Batch File in Same Command Prompt Window

I am trying to make a console-based Java application that starts some batch scripts that do some other irrelevant things. Presently, I just want to find the proof of concept
I have tried to use the following code:
Runtime.getRuntime().exec("cmd /c start pathtomybatch.bat");
This works fine until I turn it into a .jar file and attempt to execute it. Then it opens the batch file in a new command prompt window, which I don't want it to do. I want to open the batch file in the same window that my Java program is running in. I read about the start command on TechNet and SS64 and found out that apparently adding changing start to start /b would open the program in the same command prompt window. However, when I try to run this:
Runtime.getRuntime().exec("cmd /c start /b pathtomybatch.bat");
NetBeans says BUILD SUCCESSFUL for both lines of code, but when I try the second line of code, no command prompt window opens and my batch file doesn't get started.
I want to know how I can make Java open that batch file within the same command prompt window without stopping the Java application or waiting for it to finish.
Also, as a tiny extra request, could someone tell me if I could do the same for an .exe file?
I'm on Windows 7, but I want this app to work for people using Vista or newer.
The extra window is coming from the start command you initiate. See https://www.windows-commandline.com/cmd-start-command/
A better pattern is to use
Process p = Runtime.getRuntime().exec("cmd /c pathtomybatch.bat");
Then make sure you loop until p.exitValue() no longer throws an exception (which means the process has exited), and while looping copy all available bytes from p.getOutputStream() and p.getErrorStream() to System.out and/or System.err.

C# program wont be executed from Java

I have this C# program i made and while i can run it fine by clicking the exe file or by clicking on a batch file, I cant start up the program on a java program I made to run it. I have tried this line of code and couldn't get the software to run.
Runtime.getRuntime().exec("nameOfTheExeFile");
or set it to the batch file i made that starts the program.
Runtime.getRuntime().exec("nameOfTheBatchFile");
Now the interesting thing is when I try it with the batch file i get an error saying that the file cannot be found but when i double click the batch file it will start the exe file just fine.
I have even tried to use Process but I am not getting any luck with that process as well
List cmdAndArgs = Arrays.asList(new String[]{"cmd.exe", "/c", "ProgramName.exe"});
ProcessBuilder pb = new ProcessBuilder(cmdAndArgs);
Process p = pb.start();
Strange thing is i dont get any error at all. Even when i try unit testing i don't any error's at all. Is there a process I am missing or something ? I am lost on what to do.
Update:
When i check on the task manager i can see that the program is running but not the exe version. I see ProgramName.vshost.exe , is there a reason for this to be showing and not the exe file ?
Since your program is command line program you need to start it from cmd. I'm not sure if this is the best way to do it, but it works.
Runtime.getRuntime().exec("cmd /c start nameOfTheBatchFile");
Batch file:
start cmd.exe /k "nameOfExeFile"
exit

Timeout time ignored when Sikuli/Java program ran from command line

I have written a short Sikuli script utilizing Java in Eclipse. Originally I created a swing GUI to begin execution, and this fires off the Sikuli script as intended (Waits for images, finds them, clicks them. Timeouts after 90 seconds if not found). Now I'm trying to get it to run from command line while taking in an input file as an argument. The command I'm using is below:
java -jar SikuliProject.jar intputFile.xlsx
I'm seeing my print statements in the commandline, so I know it's running the script, yet it's returning an image found error without waiting the expected 90 seconds :
[error] Region.exists: seems that imagefile could not be found on disk
Is there any reason why the program ignores wait times and can't seem to find images when run this way via commandline opposed to executing from a GUI / IDE?
Thanks,
Andrew

How to provide input to a java program via a .sh script file

I have 3 Java programs that needs to run on UNIX. So I will open up a terminal window each time to run each Java program. But the input to any one of them is to be provided at another terminal window via executing a .sh file.
i.e. All the 3 Java programs need to expect input from the standard input which is to be provided via another terminal.
How can I achieve this?

Runtime exec pauses half way through execution

I'm running a Java (.jar) program from a bat script that near the end has the following (on windows)
Runtime.getRuntime().exec("svn co http://myrepository.com/someproj");
Thread.sleep(20000);
It checks out about 1/10th of the directories and 1 file and then it does nothing. I suspect the reason it is pausing on the file but I couldn't find anything. The bat script is run as an administrator.
A common reason for an exec command to block is that it is either waiting for input from the parent process (or the console), or blocked because the parent process is not reading the child processes standard output or error stream.

Categories