This is my first question here,so pardon if somewhere i go wrong.
i am trying to call a shell script on the server from the html page using Ajax.
The Ajax function is calling the java action and this java action class is invoking a shell script.
The shell script will be writing the outputs as it runs.
I want to show that output on the browser in the same way it appears on the shell.
the way i am doing it now is writing the output of the shell in a file and then show up the content of that file on the web page,but all i want is to show the outputs on the webpage as soon as the scripts writes it .
The script takes 2 minutes to run and while running ,it keeps on writing the outputs on the shell.
Thanks
One way would be to start() the shell script via the ProcessBuilder class.
Once you have the Process instance, you can obtain its standard output and standard error streams via getOutputStream() and getErrorStream() respectively.
These InputStreams contain the output you are looking for. As you read them you can then write their output to your servlet or JSP's response output stream. You'll need to do something like waitFor() the process and create thread(s) to read its output and write to your servlet's response.
Related
I am trying to call WMIC.EXE from Java to acquire battery information.
Here is the source in JavaX (an extended Java dialect): http://tinybrain.de/1001824
To see the equivalent Java source, look here (just the first 60 lines are important, really).
You can run this program with x30.jar - or by just compiling and running the Java source.
Basically it just produces a process (via a .bat file) and gets its output. The "backtick" function works fine with everything else, including stuff on Windows. Just not with WMIC - everything hangs.
The command run is this:
WMIC Path Win32_Battery Get EstimatedChargeRemaining /Format:List
Running this in the command prompt works fine... just not in Java. Java is weird with subprocesses, really.
Any ideas? Thanks...
I believe this is a duplicate of https://stackoverflow.com/a/13367685/3196753.
Quoting:
You will need to get and close your OutputStream before getting and using your InputStream. That will confirm to the process that you've started that you have finished sending input (in this case, no input) to the process.
p.getOutputStream().close();
Remember that on the Process object, getInputStream() input comes from the output stream of the process, and getOutputStream() output goes to the input stream of the process.
I have a php-script which executes a .jar-file:
<?php
passthru("java -jar nlp-server.jar 9000");
?>
I want to display the output generated by this .jar-file on a website. The problematic part is the fact that the .jar-file doesnt finish executing because its a server-application.
Thus navigating to the php-file wont help and I didnt manage to make AJAX work either.
Is there a way to display the "live" output of the .jar-file on a website?
You have to use
system();
system() is just like the C version of the function in that it executes the given command and outputs the result.
http://php.net/manual/en/function.system.php
Use it directly via (if you can) : <applet code="test.class" archive="nlp-server.jar" width=120 height=120>
This is the only way I know because the (java) script must finish in order to display results via PHP else you will get execution timeout...
I need to run a PHP script from a Java class and get the output of the script in order to notify the end user of the results. I will use Runtime.getRuntime.exec("php "); in order to execute the PHP script, but how to capture the console output?
Any suggestions?
The output of the process can be found by using getInputStream on the returned Process from the exec call.
I have a log file that is going to be updated by a shell script. This shell script has a number of operations and updates the file after each operation, saying the operation has finished. Now, I need to 'listen' on this file from a servlet and send response back to the end user in the same fashion as the logging happens (i.e. operation A finished, operation B finished and so on). Now if both the servlet and the shell script try to open the file at the same time I am sure I will get some error. In java I guess I can handle it as IOException and keep trying to read the file, so that it works when the shell script is not updating the file. How should I handle this in shell script? Will it help if I open the file in read only mode in java? Also note that the shell script only writes and doesn't read and the servlet only reads and doesn't write.
Also, suggestions welcome on a better way of implementing this workflow.
Are you using Java 7? If so then maybe the new Watcher service would work for you. I haven't personally used it but the idea is that you get notifications in your code when a file/folder has changed. This might make your code cleaner than simply polling a file repeatedly.
http://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html
Since you can't use the WatchService, you could poll the files last modification using file.lastModified().
If you do this periodically, you can compare the results and if they changed, the file was modified by the shell script. It might be necessary to create a new File object everytime you poll, but since the file isn't opened for reading at all no access problems will occur.
However, even if you open the file and compare its contents you should not experience any access problems, unless your shell opens the file with exclusive access.
I'm writing a shell script that's supposed to do the following.
- run a Java application that produces output
- run a shell command that produces output
- gather both outputs and send them out in an email
I have control of the source code of all the steps above.
Is there a best practice in gathering output from different sources? Should I redirect everything to a single temp file? Should I write different output to different files then concatenate them? What are the pros and cons of each approach?
If you don't worry about using just Java you could use the Runtime class to execute the shell command withing java (through exec) command, this will return you a Process object on which you have either getInputStream and getOutputStream so you will then be able to process the output of both the Java program and the shell command inside just one place and do whatever you want (keeping it in memory and directly send it by redirecting the outputstream to the inputstream of what you use to send the mail, with another exec) or saving it or whatever.
I'd favor using a second wrapper script which
calls the java program
calls the shell script
Captures output to a single file
reformats that output
suitable for mailing out Actually
does the mailing
Assuming you are using a unix shell, mailing/formatting and shell script calls are much simpler from the command line.