I am working on a java based desktop application. One requirement is that if application is running and user try to start the application again it should do something like show some alert etc.
I used file for this, when user run the app it add some text into a file and save it somewhere on the local system and when user try to run the app again i check the file and handle the situation accordingly, but there are some issue with this, like if application is abnormally terminated i am not able to remove that file and in that case when user tries to run the app system shows alert that application is already running but actually app terminated abnormally.
I did some R&D and found that i can read windows task manager list and then i can get and check my app if it is running. it was working fine but during testing i got an issue i-e there is a software/utility in windows named "tasklist" go to "start --> run" and type tasklist it will show you the task list. if this utility is not installed on your system or removed/corrupted for some reason then this will not work and same issue occur that user can start the application more then one time. Here is code to access the taskmanager items
Process p = Runtime.getRuntime().exec(
System.getenv("windir") + "/system32/" + "tasklist.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((line = input.readLine()) != null) {
/*
Make the comparison here and show alert.
*/
}
Question: What is a good way to check if the app is already running? I am using Java for application, and advanced installer to create the installer of the application.
Launch the app. with Java Web Start and use the SingleInstanceService. See the demo. of the SingleInstanceService for (demo. and) example code.
am using .. advanced installer to create the installer
Note that JWS:
Is also an application installer. So I am suggesting this route as an alternative to using 'advanced installer'.
Is provided by the makers of Java (Sun/Oracle).
Check out http://ss64.com/nt/wmic.html
and try
String line;
try {
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='THE NAME OF YOUR PROCESS.exe'");
oStream .flush();
oStream .close();
while ((line = input.readLine()) != null) {
System.out.println(line);
}:
input.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Courtesy:How to detect via Java whether a particular process is running under Windows?
Related
Hello guys im writing a web app in java with servlet, but i need for a job to use python, so im using Process.getRuntime().exec() for call the script.
My web app is a survey and between client compile it we take a photo of him.
I need python for deepface, for detection his emotion, and write all the results in a pdf file (what he choose, photos and detection of emotion result).
For 7 question in the survey the script works fine, when i put 8 question he never stop his job (the script working when isn't called from java i tested it).
Can you help me for understand how i can find the error? This process has got a limit of resources or something like that?
Process p = Runtime.getRuntime().exec("python "+rootPath+"\\DeepFaceLearning\\TestFace.py "+nomeFile);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String temp ="";
while((temp = stdInput.readLine()) != null) {
System.out.println(temp);
}
For python the script is very long, but i think the problem is java beacuse he works fine when i run the script from command line with 40 questions.
I need to add 21 question in total.
Script python:
take a file read it and save the questions,reason e photos in a variables
analyze all photos
wirte all this information on a pdf
save pdf in db
Edit: java enter into script but don't complete all the job.
If the your python is working for 7 the same way as for 8 question, you could try to read the inputstream in other Thread.
Like:
...
new Thread(new Runnable(){
#Override
public void run(){
BufferedReader stdInput = new BufferedReader(newInputStreamReader(p.getInputStream()));
String temp ="";
while((temp = stdInput.readLine()) != null) {
System.out.println(temp);
}}).start();
I'm trying to interact with a CLI on a server from a web browser. On the server side, I'm using a Java Servlet running on JBoss AS 7.1.1.Final. The CLI is an ovirt-engine tool (ovirt-iso-uploader). In order to use it, you have to provide the REST API password when it requests it.
Therefore, here is the following code I'm using to interact with the CLI:
private String executeCommand(String command) {
System.out.println("Executing command: " + command);
String[] commands = new String[]{"/bin/sh","-c",command};
try {
ProcessBuilder builder = new ProcessBuilder(commands);
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
System.out.println(line);
if (line.contains("Please provide the REST API password")){
writer.write( "password\n" );
writer.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
However, whatever input command I'm using, I always have the same problem. (For this example I used the command ovirt-iso-uploader list to list all the ISO storage domains. You can find the documentation of the CLI here)
11:31:34,282 INFO [stdout] (http--0.0.0.0-8080-2) Executing command: ovirt-iso-uploader list
Please provide the REST API password for the admin#internal oVirt Engine user (CTRL+D to abort):
So the execution is blocked here as the CLI is waiting for the password and the Servlet can't see the line Please provide the REST API password for the admin#internal oVirt Engine user (CTRL+D to abort): as it is not send in [stdout]. However, it is working if I manually enter the password directly in the terminal.
Therefore, my question is how can I read the password request from the CLI and answer to it ?
Thank you for your time.
I would suggest that buffered reader is the wrong thing to use as it may be waiting for a newline at the end of the prompt which is not there and can not therefore return a line.
I think you will have to read the stream directly. Maybe character by character or byte by byte.
It may also not be necessary to wait for the prompt. In unix shell programming it is common to provide the standard input without reference to prompt like:
Command <!
Lineone
line two
!
I have a java servlet running in a server, plus an 'exe file' located in the same server,
i want , in respond to the client passed parameters to the servlet , to run the exe file located on the server and show it to the client , even a screen shot,,
any ideas??!! please help
You can use Process and Runtime classes
Eg :
Runtime r = Runtime.getRuntime();
Process p = r.getRuntime().exec("C:\\newfolder\\run.exe");
For taking screenshot refer to how to take sc in java
This way you can save the image and then send this image to user.
For sending image to client refer to how to send file from sever to client
these are.the pieces , you need to put them together
UPDATE 1 : to kill the exe you can use p.destroy() ( not a good implementation though, as it forcefully kills the process)
UPDATE2 : to check if the process( which is executing your exe) hence to check if the exe is running or not, you can refer to how to check if a process is running
You can run an external command in Java by the following code:
Process p = Runtime.getRuntime().exec("your_external_program_here");
You can pass in parameters as well, simply amend the above line to include what parameters you want to pass into the program.
To retrieve the 'output' of the process you need to get the input stream for the process:
InputStream output = p.getInputStream();
Note the input stream is the piped output of the process. You can then view the contents (advisable to use a buffered reader) like this:
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(output));
while ((line = reader.readLine()) != null) { ... }
Or alternatively you can look at ProcessBuilder which is easier to use :)
I'm trying to integrate with a legacy system. The legacy system uses dialog windows to report errors. It have no return codes at all except for the dialog windows. I start the legacy system with Runtime.exec().
Is there a way to detect if the executed program has spawned dialog windows or any other graphical interface? This solution is done in Windows and the executed program is an exe.
If the legacy system report errors in console, is possible get your erros.
Simply take the inputstream of error and do your reading.
Like this:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
// any error message?
InputStream error = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(error);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(" ERROR >" + line);
I recommend to read: Runtime.exec() quirks
Hope this help.
You can use this JNA snippet to poll for windows started by your process.
AFAIK, you can only get the standard and error output streams from a process using the Java Process API.
So the solution i did, is to use the snippet code that #Gerrett Hall linked to. That snippet code check the active window before i run the command and save the name.
Then after a vile if the command have not returned check if the active window have changed. if it have, kill the process(Alt. send a return key global).
To get the info from the dialog i could use Ctrl + C to copy the content of the dialog and reading the paste buffer to copy the message in to the log. Have not figured out that part yet.
And yes this is a ugly hack but so is the legacy system to.
I'm making an update function for my project, it's working great, until i want it to restart, basically I download the new file and replace it with the old one, and then i want to run it again, now for some reason it doesn't wna run, and i don't get any error...
Here is the complete update class:
http://dl.dropbox.com/u/38414202/Update.txt
Here is the method i'm using to run my .jar file:
String currDir = new File("(CoN).jar").getAbsolutePath();
Process runManager = Runtime.getRuntime().exec("java -jar " + currDir);
It's not clear to me, why do you need to run the jar with a call to exec() . Given that you need to run the code in the .jar file from a Java program, you could simply run the main() method as defined in the jar's manifest, and capture its output - wherever that is.
Using exec() is OK when you need to call a program from the underlying operating system, but there are easier ways to do this if both the caller and the callee are Java programs.
Now, if your jar is gonna change dynamically and you need to update your program according to a new jar, there are mechanisms for reloading its contents, for instance take a look ath this other post.
The JavaDocs for the Process class specifically point out that if you don't capture the output stream of the Process and promptly read it that the process could halt. If this is the case, then you wouldn't see the process that you started run.
I think you have to capture the stream like this :
BufferedReader stdInput = new BufferedReader(new InputStreamReader(runManager.getInputStream()),8*1024);
BufferedReader stdError = new BufferedReader(new InputStreamReader(runManager.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
The exec function doesn't automatically lookup into the PATH to start a process, so you have to pass the complete path for the java binary.
You can do that by using the java.home system property, see this answer: ProcessBuilder - Start another process / JVM - HowTo?
No one here seemed to help me, so I went to ask my friend and I had it almost right. It abiously required the string to be an array.
solution:
String[] cmd = {"java", "-jar", currDir};
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
e1.printStackTrace();
}