Please suggest if there is any best way around on executing jar from java code then killing parent java code.

a)I have desktop based java application say "Monitor.java" which runs every 5 minutes.

b) How can I START external java application say "execute.jar" from Monitor.java THEN EXIT/KILL Monitor.java

I tried various options using "ProcessBuilder" and calling bat file but I need Monitor (parent application to EXIT, immediately after calling child (execute.jar)

Try1) 
ProcessBuilder builder = new ProcessBuilder("java -jar execute.jar");		    
Process process = builder.start();
--------------- 
Try2) 
Runtime r = Runtime.getRuntime();
Process p = null;
p = r.exec(new String[] { "cmd", "/c", "start C:/temp/Test.bat" });

I've never tried it.
What happens when the Monitor class does a System.exit() after starting a process?
Is the process ended also?

Quick test shows it exits the monitor application..
Process p = Runtime.getRuntime().exec("C:/WINNT/system32/notepad.exe");
System.out.println("Exiting...");
System.exit(0);

I will do some real time test and get back...

Thanks

it exits the monitor application..

yes I expected that. But What happened to the process that was started.

Hi,

Thanks for your time...

Here is the code which first finds exec process id, then KILLS it, then Starts exec jar.

Please check "startExecutorJar()" which is used to start exec.jar from Monitor application code, I am simply using System.exit(0); which exits monitor application after starting "exec.jar".

I have commented the code "process.waitFor()" as we don't want any return result from child process.

Please suggest if you feel anything is wrong or can be improved.

Regards
Val

package com.Test;

public class MonitorKillAndStartExec {
	
	public static void main(String[] args) throws Exception {
		MonitorKillAndStartExec mp = new MonitorKillAndStartExec();
		String execPId = mp.readExecPId(System.getProperty("user.dir")).trim();
	
		if (execPId.length() !=0) {
			//KILL EXECUTOR
			killExec("taskkill /pid " + execPId);

			//START EXECUTOR
			startExecutorJar();
		}else{
			System.out.println("Executor Pid not found, No need to Kill");
		}		
	}

	//START EXECUTER JAR AND EXIT MONITOR i.e. this APPLICATION
	public static void startExecutorJar(){		
		try{
			List<String> command = new ArrayList<String>();
		    
		    command.add("java");
		    command.add("-jar");
		    command.add("C:\\test\\exec.jar");
		    
		    ProcessBuilder builder = new ProcessBuilder(command);		    
		    Process process = builder.start();		
		    
		    System.exit(0);
		    
		    /*InputStream is = process.getInputStream();		    
		    // any error message?
		    StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");      
	        // any output?
		    StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT");
	        
	        // kick them off
	        errorGobbler.start();
	        outputGobbler.start();
	        
	        // any error???
	       int exitVal=-1; 
	       try {
	    	    exitVal = process.waitFor();
			} catch (Throwable t) {
				System.out.println("Executer, In catch");
				t.printStackTrace();
			}*/
    		
		}catch(Exception e){
			System.out.println("Executer threw a SQLException : " + e);
			e.printStackTrace();			
		}finally{
			
		}
		
		System.out.println("Exec FINISHED");
		
	}

	public String readExecPId(String fFilePath) {
		StringBuilder executorPId = new StringBuilder();
		Scanner scanner = null;
		try {
			scanner = new Scanner(new File(fFilePath+"\\"+"execPId.txt"));
			while (scanner.hasNextLine()) {
				executorPId.append(scanner.nextLine());
			}
		}catch(IOException ie){
			System.out.println("MonitorKillAndStartExec.readExecPId() could not find : " + fFilePath+"\\"+"execPId.txt");
		}finally {
			if(scanner!=null)
				scanner.close();
		}
		return executorPId.toString();
	}
	
	private static ArrayList<String> killExec(String processStr) throws IOException {

		String outStr = "";
		ArrayList<String> processOutList = new ArrayList<String>();
		int i = -1;
		
		Process p = Runtime.getRuntime().exec(processStr);
		
		// OutputStream out = p.getOutputStream();
		InputStream in = p.getInputStream();
		
		x11: while ((i = in.read()) != -1) {
			if ((char) i == '\n') {
				processOutList.add((outStr));
				outStr = "";
				continue x11;
			}
			outStr += (char) i;
		}
		
		return processOutList;
	}

}
This article has been dead for over six months. Start a new discussion instead.