I have a java file which i want to execute repeatedly on a machine. The file should be executed automatically as soon as the previous instance of the file execution is over. How to achieve this?
Use bash script in a loop call your executable
for i in 1 2 3 4 5 .. N
do
java executable
done
Java provides an environment in which a program will function as you (or someone else) have written it.
The operating system specifics will determine how that program is started. If you want the program to launch each time the previous copy has finished, you need to wrap the launching of the java program with a loop in the scripting language supported by that operating system, or convert the program to be a system service / daemon.
Other techniques exist, but they actually don't launch the second program after the first completes. They either:
Launch a second copy of the program immediately (unless already found). The program is written to do nothing while it can detect it's "launcher" is running. Upon loss of one process, the second becomes responsible for the processing (and the launching of it's standby failover).
Capture shut down / crash events and launch a copy of the program when quitting, which actually has two programs running at the same time, but for a short duration. This is commonly done; but is technically unsound as there are many ways a program might crash before being able to launch a new process.
You can write a simple perl or python script that would work on both windows and linux.
#!/usr/bin/perl
$N = 100;
for($i = 0; $i < $N; $i++){
system('/full/path/to/java -cp full_class_path my.class.Name');
}
You can call this script in a java file or execute it on command line.
Runtime.getRuntime().exec("perl script.pl")
Related
I'm making a little tool that handles a sort of exotic device, with lots of options to manage it etc. One of them would be to scan for tty ports, find the right one then throw the user to "minicom" with some parameters for example.
How can I make java completely exit while running a specific command, under certain condition, after such exit ?
Initial thoughts would be to use a bash script, scan the return value, communicate via files etc.A fully functional interactive serial console in Java would be the dream, but the ones I try right now can't seem to even find tty ports now.
Most processes on linux follow a call stack where process A calls process B which calls process C.
Wen process C terminates, the control goes back to process B, and so on.
It sounds like in this case you want java to call minimum, but when java is finished, return to the parent shell.
I am not aware of any way you can terminate a JVM upon a call to another process (returning to the JVM's parent when it terminates). Perhaps with some clever C calls using JNI, but that isn't really java anymore and could create new problems.
You could have the JVM wrap the target process and pass through the user inputs and outputs. Alternatively, use file communication, e.g. the java program writes the command-line to a file, that the parent bash script executes after the JVM terminates, but that is a bit of a kludge.
I have a Java application that needs to run several times. Every time it runs, it checks if there's data to process and if so, it processes the data.
I'm trying to figure out what's the best approach (performance, resource consumption, etc.) to do this:
1.- Launch it once, and if there's nothing to process make it sleep (All Java).
2.- Using a bash script to launch the Java app, and when it finishes, sleep (the script) and then relaunch the java app.
I was wondering if it is best to keep the Java app alive (sleeping) or relaunching every time.
It's hard to answer your question without the specific context. On the face of it, your questions sounds like it could be a premature optimization.
Generally, I suggest you do what's easier for you to do (and to maintain), unless you have good reasons not to. Here are some possible good reasons, pick the ones appropriate to your situation:
For sleeping in Java:
The check of whether there's new data is easier in Java
Starting the Java program takes time or other resources, for example if on startup, your program needs to load a bunch of data
Starting the Java process from bash is complex for some reason - maybe it requires you to fiddle with a bunch of environment variables, files or something else.
For re-launching the Java program from bash:
The check of whether there's new data is easier in bash
Getting the Java process to sleep is complex - maybe your Java process is a complex multi-threaded beast, and stopping, and then re-starting the various threads is complicated.
You need the memory in between Java jobs - killing the Java process entirely would free all of its memory.
I would not keep it alive.
Instead of it you can use some Job which runs at defined intervals you can use jenkins or you can use Windows scheduler and configure it to run every 5 minutes (as you wish).
Run a batch file with Windows task scheduler
And from your batch file you can do following:
javac JavaFileName.java // To Compile
java JavaFileName // to execute file
See here how to execute java file from cmd :
How do I run a Java program from the command line on Windows?
I personally would determine it, by the place where the application is working.
if it would be my personal computer, I would use second option with bash script (as resources on my local machine might change a lot, due to extensive use of some other programs and it can happen that at some point I might be running out of memory for example)
if it goes to cloud (amazon, google, whatever) I know exactly what kind of processes are running there (it should not change so dynamically comparing to my local PC) and long running java with some scheduler would be fine for me
I am creating a web application where user enters a query and i have a program in perl which tag the words in query. I am writing a spring controller to handle the Web request and then call the perl program from java class. Is this the optimized way. Is there any performance issue or time delay in calling perl program from java?
Well, it depends on how you are calling the perl program. When running a perl script, the perl interpreter starts, then compiles the script, then runs it. This means everytime you run program you spawn a new process, read the text file(s) containing the program, interpret them, and then get the results.
This is not optimum.
To optimize the process, write a wrapper in perl for you perl script that runs it continuously in a loop, waiting for input from your java process via a socket, named pipe, or file descriptor. This way, the process does not have to start over and over again. To support greater concurrency, run a pool of these.
I have a matlab script that takes an input mat file and produces an output file.
Currently I have a shell script that is able to execute this script.
I am executing this script from Java using ProcessBuilder class and all is working well.
I need to perform the matlab script execution very often from java and each time script executes the mcr process is created, script executes and mcr terminates.
i like to have the MCR process open and have a matlab session open towards java , so that same process is kept alive so i can execute scripts multiple times in the same session.
I find matlabcontrol seems to be suitablefor this.
How to configure the MatlabProxyFactory with the MCR location and LD_LIBRARY_PATH
which i am setting in my shell script and execute my script in a same session?
I also came across a tool MATLAB JA Builder but i cannot use this at the moment as its not Free.
Thanks!
If you are using matlabcontrol I don't think you need the shell script, just tell it the name of the m-file you want to be run. If you want to keep the shell script in conjunction with a ProcessBuilder, you have to start the process once and keep a reference to its input stream. When you want a command executed, you write it that stream. Also, you have to keep a thread alive to empty the output and error streams of Matlab, otherwise they will get full and Matlab will hang. If you want to go with this, I recommend looking at the source of this project (which I am a contributor to). The class you are interested in is RCaller. It does more or less the same, except it invokes R and not matlab.
I've a bash script that sequentially calls a java program. That's a bit tricky but mainly what i do is to have a loop and executes about 1500 times the same java program with different arguments.
My question is, when using java 1.5 (the sun VM), each time I'm calling the java program a new instance of the jvm is created ? (I'm not sure that's the right vocabulary...)
Should I avoid this situation by introducing a level of indirection, i.e building a list of all the parameters and then executing one java program which takes these parameters and executes what was previously my entry point ?
Or can I deal with the problem by configuring the JVM as resident or something like that and dynamically invokes my program....
hope this is clear....
thx...
You could save the parameters into a file and use the Java program to process it without constant restart. You could also pipe in the parameters into the running Java app through the console, similarly as for example ls | grep java
Edit: And for the first question. I doubt the java runtime would stay deliberately in memory. Probably most JRE files would remain in the disk cache anyway. On Windows there is a Java Quick Start service which keeps the JRE files around to reduce the startup time for a java program. Don't know whether there is a similar thing for *nix.
Obviously having all the parameters beforehand and running the program once after that would be the best solution. If you cannot do that for any reason i have a very dirty solution to this. Have your program register a port and listen to it for input. Then simply pass the arguments to that port and have your program handle them as a new instance.
JVM startup is notoriously slow, and it certainly not intended to be done in a loop like that. Unfortunately the only way to avoid this if you are passing command line parameters to the java program is to modify the java program itself in some way to have alternative forms of interaction (either from the console, or a port, or a read a file). Java Quick Start is the only (closest thing to a) solution if the java program cannot be changed.
The real solution is to change the Java program. The most obvious change would be to have your loop write to a file, and then start the java program that would read the file one line at a time. That works if the loop doesn't care about the results from the java program for the next set of parameters.
If it does, then it would really be necessary to understand that relationship to advise on an appropriate solution. The socket solution suggested by Savvas is certain a general purpose solution, but there may be better options, depending on what you need to accomplish.
You can use a launcher like in the answer to
Simultaneously run java programs run on same JVM? to read input line by line and start your program's main() method.