Is there a way I can start a command-line application from java and then send strings (commands) to its input stream and display its response from its output stream?
I'm using an application with a pretty sophisticated command line interface (vlc). The application has an interpreter that responds to a set of commands. For example, after I start the app, I can start or stop a movie by issuing the command 'pause' on the command line.
I'd like to write a java application that executes the program and issues commands to the program. I've seen many examples of java apps starting an application and getting the output stream of the app displaying the output of the app. But I've never seen an example, in which the java app would send requests to the sub-application.
Is there a way I can do this using java?
Thanks in advance!
So long as the spawned process listens on stdin for input, sure.
You'd launch a Process in the usual way (Runtime.exec()) - I won't document it here, as you say you've seen plenty of examples.
Then once you have a handle to the spawned process, you call the confusingly-named getOutputStream. This gives you an OutputStream, the other end of which is connected to the process' standard input. Hence, any bytes written to this stream can be consumed by your child process, just as if you were typing/piping input from a console.
I will point to a couple of resources that are always worth reading when dealing with Processes; cut-and-paste jobs from arbitrary Google results often don't cover the edge cases properly and can lead to deadlocks:
When Runtime.exec() won't (old, but still relevant)
Five common Java Process pitfalls
Related
I tried to find a solution for the following use case (on Linux):
Start the program, show some information to the stdout, input some information such as username/password.
The program validate the username/password, then goes to background and run as a daemon.
I did not find a way to do this in Java. There are several sulotions to daemonize java program (such as jsvc, or this: http://barelyenough.org/blog/2005/03/java-daemon/ ). But seems they all do not work for this situation, because the program just goes to background from the beginning, there is no chance to input information before it goes to background.
I don't believe that there's a way to do this purely in java. You could make it work by writing an init script which accepted the command line parameters before spawning the java process in the background. You could use -D command line arguments to pass the user input to the java process.
I have created a Java Web Application that asks the user to upload an ARF File, converts it to MP4 and saves it on the server and the user is then sent an email with the link of the new MP4 file. The conversion takes place by calling a VB Script from the command line. This script converts all the ARF files that are located in a desired directory to MP4 format.
The application has been working well but I have noticed one thing. It does not work when one person (using one computer) runs the application and say the file is in the process of being converted (i.e. the user is waiting for the email). At this stage if another user (using another computer) tries to run the application at the same time, the conversion process gets disrupted and it stops the previous conversion.
I tried testing the VB Script by running it, waiting for a video to be converting and then running it again. I noticed the same thing.
Was wondering if there is a way to make my application concurrent so that it can run independently i.e. more than one person can run it at the same time.
Thank you
The limitation here is the limitations of the VB script. Can you (for example) generate a different filename for each so that it works in parallel?
If not then you will have to queue up the requests and then have some code that processes the next request from the queue one at a time.
It sounds like the code that invokes the VB script is the bottleneck. I take it that the file conversion process would take some amount of time so that what you want is have the processing started in a background thread and terminate the java session. The first thing that came to my mind is Quartz job framework. You can trigger a Quartz job that does the processing and emailing and each Quartz job is running on its own thread. http://quartz-scheduler.org/documentation
I have created and am working on a server-application that monitors for specific folders and takes appropriate actions whenever files are being added.
Now I come to the point where I want to be able to shutdown the program, for example for applying a patch.
The server runs simply in a command prompt, how can I signal that I want to perform maintenance on it? I do not think reading System.in is feasible as I am also outputting text in the prompt.
Regards.
You could try reading System.in as System.in and System.out are different file descriptors. What this means is that by writing things in console you are not writing in the same place than when you are typing, so console output should not matter for reading commands in the prompt.
A second application can be used to communicate with the server application. You can use Java Management Extensions or implement your own client/server communication using sockets.
Another way to achieve this is that server periodically checks for existence of an specific file somewhere on hard disk. If server finds that specific file, it will shut down.
Can I call a Java program from a Node.js application on Heroku?
I have a Node.js/Heroku app. But now need to add server-side capability to run an algorithm on an input data file and output data to a JSON format. I already have a Java library that can read the file and run the algorithm, and it would be very difficult (at best) for me to re-write it in pure Node.js.
So, could write a command line program, that takes an input file and pipes the results to stdout, e.g.
java mytask.class -cp ./mylibrary.jar --in /tmp/file.in > output.json
Is it possible to shell out a call to a Java command line program from Node.js? I know one can deploy Java applications to Heroku, but here want to execute a bit of Java from a Node.js app.
Don't you want this and child_process.exec() in particular ?
Node provides a tri-directional popen(3) facility through the
child_process module.
It is possible to stream data through a child's stdin, stdout, and
stderr in a fully non-blocking way.
Note that your example command above isn't right, since you're trying to pipe to a file (output.json). Pipes only work between processes. The child process module would allow you to read the processes' stdout directly and you wouldn't need the file (similarly for the input stream)
I have a R script I need to call from Java and run. I tried this code: Runtime.getRuntime().exec("Rscript pathTo/R/myScript.R"). I run it from windows command it worked fine, but when I run java class with this code in Eclipse, nothing happens. The console doesnt show anything no error no logs. Can someone tell me how to run this script from Java?
By default, a Process launched from java has its standard input, standard output and standard error redirected to pipes, which you can access from within java. Unless you read from the standard output and error pipes and transfer the text to the output of the Java application yourself, no output will become visible. Furthermore, if the internal buffer of the pipe gets full, then the child application might even block while waiting for root to write its data. So the process probably will hang and never terminate.
Since Java 7, you can have the child process inherit its I/O channels from your Java application using ProcessBuilder.inheritIO. That saves you all the trouble to read from those streams yourself.