I have a java program that opens up a .exe file. What i want to do is,
1)Run the .exe file when the java program is executed - Runs perfectly
2) When running the same code again, I want to reopen the previously executed exe.I dont want the .exe file to execute again.
EDIT:
Is there a way to do the above? My only concern is the 2nd question. I have made the 1st question to work
On the request of Luffy. This is the code that i had used to get the list of running process and check whether a particular exe is running or not. Hope it helps for others.
try
{
String line;
String pidInfo = "";
Process proc = Runtime.getRuntime().exec("wmic.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
oStream .write("process where name='test.exe'");
oStream .flush();
oStream .close();
while ((line = input.readLine()) != null) {
pidInfo+=line;
}
input.close();
if(pidInfo.contains("test.exe"))
{
System.out.println("exe already running");
}
else
{
System.out.println("exe not running");
Process p = Runtime.getRuntime().exec(new String[] {"C:\\Program Files (x86)\\ToTestEXE\\test.exe"});
}
}
catch (IOException e)
{
e.printStackTrace();
}
Related
I am trying to print the output of a shell script on the console using java. When I manually run the script, I get
C:/Users/user1/Desktop/shell.sh: line 78: /usr/ucb/ps: No such file or directory
<STATUS>: Probe [ devicename ] is not running!
But, when I try to run it on my Java program, the output is not being printed on the console.
My code is:
ProcessBuilder processBuilder = new ProcessBuilder("C:/Program Files/Git/git-bash.exe","C:/Users/user1/Desktop/shell.sh");
try {
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
System.out.println(line);
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println(output);
System.exit(0);
} else {
//abnormal...
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
The only output I am getting is "Success". When I debugged my code, I found that the code never enters the condition
while ((line = reader.readLine()) != null)
even though, in the bash terminal, there are lines of output. Why is this happening?
I am stuck at this point and I couldn't find any other explanations for this problem. Kindly help.
You are running git-bash.exe which opens as a windows application. Although Java has access to the stdout/stderr streams of git-bash.exe, these are not necessarily the same as the stdout/err of the internal launch of your shell script within git-bash.exe.
One way to see the stdout/err of your command would be to make a java friendly version of the .sh script which launches your original sh and redirects output to a specific files which you can then access within java afterwards.
ProcessBuilder pb = new ProcessBuilder("C:/Program Files/Git/GIT-BASH.EXE","/c/Users/blah/somescript.sh");
somescript.sh:
#!/bin/sh
runtheoriginalcommand > ~/somepath.out 2> ~/somepath.err
You could also add extra args to wrapper to pass the out/err files to be used so there is no contention with any other launches or hardcoded output files.
I have a VBS file xxx.vbs that i'm able to execute it on my local machine using the below code:
String cmd = "wscript filePath\\xxx.vbs";
Process p = Runtime.getRuntime().exec(cmd);
But when i create a war file of the project and deploy it on the server, I'm not able to execute the vbs file.
I can however execute the VBS file manually on the server. So there is nothing wrong with the VBS file.
Any idea on what may be the reason for the above?
Try adding the below snippet to determine whats happening in the background.
String line="";
try
{
InputStreamReader isr = process.getInputStream();
BufferedReader br = new BufferedReader(isr,4094);
while ( (line = br.readLine()) != null)
System.out.println(line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
try
{
isr = process.getErrorStream();
br = new BufferedReader(isr,4094);
while ( (line = br.readLine()) != null)
System.out.println(line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
P.S. The above snippet is just for informational purpose.
Runtime.getRuntime().exec() will simply begin the execution, and exits..
Trying the above will eventually show you the exceptions, if any, and the output of the script file you are trying to run.
However, the better way is to create a seperate runnable which can accept the input stream and display its content instead of writing the code I specified above.
I want to run c++ .exe file from java and also want to print the output of .exe file.i tried and succeed to run c++ .exe file from java ,but i am not getting how can i print the output(in java output field) of c++ .exe file using java,i tried using processExitValue and waitfor methods but not getting desired output.The java code is here
int processExitVal = 0;
try {
Process p = Runtime.getRuntime().exec("cmd /c start rs.exe");
processExitVal = p.waitFor();
// p.getOutputStream();
//InputStreamReader ir=new InputStreamReader(p.getInputStream());
//BufferedReader t = new BufferedReader((new InputStreamReader(p.getInputStream())));
// int y=Integer.parseInt(t.readLine());
InputStream in=p.getInputStream();
System.out.println(in.read());
//System.out.println("output"+Process.getOutputStream());
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("InterruptedException");
e.printStackTrace();
}
System.out.println(processExitVal);
System.out.println("Execution complete");
}
I will be thankful if u will help me out this problem. Thanks in advance
You could use a Scanner and then read lines from it. Are you sure your process is writing something on the standard output?
Edit:
read(): Reads the next byte of data from the input stream.
You have to use a Scanner:
Scanner scan = new Scanner(p.getInputStream());
String line = scan.getNextLine();
Regarding Deestan's answer: getInputStream() is the correct method here, we want the output of the process, that's an input for the application.
Use "rs.exe" instead of "cmd /c start rs.exe"
As example:
Process process = Runtime.getRuntime().exec("rs.exe");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = in.readLine()) != null) {
System.out.println(line);
}
This code works fine on Computer A and doesn't work on Computer B ... I can't understand Why..
Nothing Exceptions or anything else ....On Computer A log file was created , on computer B log file was't created.
Computer A and B have the same Java version...
Do you have any ideas?
String str = "cmd /C dir tools>1.log";
try {
Runtime.getRuntime().exec(str);
} catch (Exception e) {
e.printStackTrace();
}
PS
this code works fine on both computers
String str = "cmd /C dir tools";
You have to open the process' output stream to save the output to a file correctly.
You can do this by creating a Process object and saving that to a file:
Process p = Runtime.getRuntime().exec(str);
InputStreamReader reader = p.getInputStream();
BufferedReader buffer = new BufferedReader(reader);
String line = null;
while ((line = buffer.readLine() != null) {
//write stuff to file here
}
Going on what SLaks said -- it is your best bet to use the built-in file APIs.
Here is a link for a general tutorial
Using these APIs will take out any strange environment issues from Computer A to B...to C and so on.
I'm trying to edit a file from CLI. I'm executing the nano command (I know that command will always be available); when I execute it, I can see nano's output but I cannot interact with it. How can I pass user input to the command? Do you have a better idea to easily edit a file from within my Java app?
This is my code:
String command = "nano /tmp/163377867.txt ";
try {
Process process = Runtime.getRuntime().exec(command);
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
The problem with Java's Runtime.exec is that it connects stdin and stdout to "pipes," while many console programs need a TTY device.
One way to solve this problem is to make the Java program's controlling terminal available to the program you execute:
Process proc = Runtime.getRuntime().exec(new String[]{
"sh", "-c", command+" </dev/tty >/dev/tty"});
proc.waitFor(); // wait for user to finish editing the file