How to launch two java program in the same time through another one?
i'm a beginner and i try to code very simple java programs and here i have two java programs.
the first program for selecting recent files from a folder.
the second program for concatenating these recents files.
what i want is to create a third java program that -will launch the first program. -then wait for 10s -launch the second java program.
i want to keep separately the first and second program.
because i want to use them for a further 开发者_Go百科application.
is it that possible? Do i have to use Process method like if i want to launch notepad.exe through java? or else?
Thank you
There is nothing special about the main method. You can call it directly from your third program if you like.
public static void main(String[] args) throws Exception {
Program1.main(args);
Thread.sleep(10000);
Program2.main(args);
}
If you want to launch them as separate java processes, you should use the ProcessBuilder
. You can also do this in groovy, very simply as:
"java <arguments>".execute()
Otherwise you can "launch" it by calling the main method as suggested by @Heathen, though they would both be running in the same JVM with that method.
You can use Process method as you described, but it isn't a wise solution to rely on an arbitrary timeout for the first program finishing. Instead you should wait for the first one exiting.
By the way, you could generate two jar files, one for each job. You then can easily create one application out each one of them and you can easily combine them to create a third application for the combined job. This is much better than creating processed from Java.
ProcessBuilder builder1 = new ProcessBuilder("firstProgram", "arg1", "arg2", ...);
ProcessBuilder builder2 = new ProcessBuilder("secondProgram", "arg1", "arg2", ...);
Process proc1 = builder1.start();
//Pauses for 10 seconds
Thread.sleep(10000);
Process proc2 = builder2.start();
//This next part is necessary to wait for the programs to finish. If you want them to
//run independently of the main thread, you can omit this part
int exitVal1 = proc1.waitFor();
int exitVal2 = proc2.waitFor();
You can get to the input, output, and error streams of a Process object, useful if you want to interact with the running process.
You can find more information about the Process object at http://download.oracle.com/javase/6/docs/api/java/lang/Process.html
For example my code is:
import java.io.IOException;
public class Launcher
{
public static void main(String args[]) throws IOException, InterruptedException
{
try {
Process[] proc = new Process[2];
proc[0] = new ProcessBuilder("calc.exe").start();
Thread.sleep(3000);
proc[1] = new ProcessBuilder("D:\\NetBeansProjects\\GetIPAddress\\dist\\GetIPAddress.jar").start();
try {
Thread.sleep(3000);
}
catch (InterruptedException ex)
{
}
proc[0].destroy();
Thread.sleep(3000);
proc[1].destroy();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
If i replace the proc[1] by notepad.exe it's working well
thank you
精彩评论