postgresql - complete CMD command not running from java -


i running below query through java on postgres db using psql:

psql.exe -u <user> -w -h <host> -d <db_name> -a -f <file> 2> "<path_to_file>\psql.log" 

initially, quite time java program did create file. ran problem, not overwriting log file. used file.delete() function after every time log file got created via java.

now, java not creating log file reason. if run above manually in command prompt, runs absolutely fine, not via java code. can see command getting run in java log, not create log file when have removed file.delete() function

i researched lot on not find solution. highly appreciated.

its long code..so tell relevant part.

i calling function thread. code below function:

public static void saveacopyfiletoserver(int auditid,string filepath,string fname,string tb_name,string plpgsql_path) throws exception     {         map<string, string> env = system.getenv();          string plpgsql = "\""+plpgsql_path+"\" -u "+env.get("pg_user")+" -w -h "+env.get("pg_host")+" -d "+env.get("pg_db")+" -a -f "+"\""+filepath+"copy_"+tb_name+auditid+".sql\" 2> \"c:\\er\\etl\\logs\\psql.log\"";         system.out.println(plpgsql);         process p = runtime.getruntime().exec(plpgsql);         p.getoutputstream().close();          p.waitfor();          simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss:sss");         calendar cal10 = calendar.getinstance();         system.out.println("data loaded "+tb_name+auditid+" @ "+sdf.format(cal10.gettime()));     } 

after calling function is:

public static void extracterrorreason(string fname,int auditid,string sessionid,connection con_pg) throws filenotfoundexception, ioexception, interruptedexception{     file file = new file("c:\\er\\etl\\logs\\psql.log");      if(file.exists())     {       system.out.println("file present");         }     else         {          system.out.println(file+" not found");         }     if (file.length()!=0){         system.out.println("log file being read "+file);                     bufferedreader br = new bufferedreader(new filereader(file));                     string line = br.readline();                     string out_err = line.substring(line.indexof("error"));                      system.out.println(out_err);                     system.out.println("error while loading file database file "+fname);                      string comment = "copytostage','"+out_err;                           utils.updateauditdetailtable(auditid, sessionid, -1, comment, true, con_pg,"");                      br.close();                     //file.delete();                 } } 

the first function used create psql.log file, not create it. not sure problem. every time run code , second function,i printline log file not found. part before redirection of output of cmd command works fine.

i tried process builder also..

i tried process builder

string plpgsql = "\""+plpgsql_path+"\" -u "+env.get("pg_user")+" -w -h "+env.get("pg_host")+" -d "+env.get("pg_db")+" -a -f "+"\""+filepath+"copy_"+tb_name+auditid+".sql\" 2> \"c:\\er\\etl\\psql_" +auditid +".log\""; processbuilder pb = new processbuilder("cmd.exe",plpgsql); process p =pb.start(); p.getoutputstream().close(); p.waitfor(); 

i expect problem runtime.getruntime().exec(plpgsql) splitting command line arguments incorrectly. basically, exec not understand quoting. instead, splits wherever sees 1 or more spaces ... if spaces in quotes.

the solution use exec(string[]) overload, , pass each individual argument separate string; e.g.

 .exec(new string[]{plpgsql_path,                     "-u",                     env.get("pg_user"),                     "-w,                     "-h",                     // etcetera                    }); 

update

i didn't notice using > output redirection well1.

that doesn't work exec either. (and same applies all shell syntax.) redirection, need use processbuilder , 1 of redirect methods.

the other alternative run command in shell. pass command string, , let shell take care of quote handling, substitution of environment variables, globbing, redirection ... , on.

for example (if running on unix, linux or macosx):

 .exec(new string[]{"/bin/sh", "-c", plpgsql}); 

for windows

 .exec(new string[]{"cmd.exe", "/c", plpgsql}); 

note "/c" option in windows case!


1 - serves right not line-breaking ~200 character line in source code! check out java coding standards source line lengths ...


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -