ProcessBuilder - Java program - Running Batch programs -
i have requirement create 1 java utility 1 of process in our team automated. existing process calls executable , business processing in sequence. wrote small program
fileinputstream fstream; try { string [] cmd = new string[3]; if(system.getproperty("os.name").tolowercase().contains("win")){ cmd[0] = "cmd.exe"; cmd[1] = "/c"; } else{ cmd[0] = "/bin/sh"; cmd[1] = "-c"; } fstream = new fileinputstream("c:\\javaprogram.txt"); bufferedreader br = new bufferedreader(new inputstreamreader(fstream)); string fileline,fileline1; //read file line line while ((fileline = br.readline()) != null) { cmd[2]=fileline; processbuilder proc = new processbuilder(cmd); process p = null; proc.redirecterrorstream(true); system.out.println("starting process "+ fileline); p = proc.start(); bufferedreader br1 = new bufferedreader(new inputstreamreader(p.getinputstream())); while ((fileline1 = br1.readline()) != null) { system.out.println("output"+ fileline1); } int exitvalue = p.waitfor(); system.out.println("\nexit value " + exitvalue); } br.close(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); }
the program reads javaprogram file contains list of programs executed in sequence. have following queries:
- the process runs in background , monitor, had check in task manager. there way process can run foreground.
- there cases in program results in error. correct way capture error i.e. based on
waitfor()
output. current logic displayed value[which enhance later display proper error message] - is way best way handle requirement[please ignore basic redundancy /extra processing @ point of time in dev phase]
- some programs execute require administrator priviledges. code required
- any other things taken care of??
Comments
Post a Comment