I need to set an environment variable during runtime and fetch the value of that variable in UNIX environement using JAVA . Given below is my code.But, it's not able to fetch the environment variable and process output is not getting shown in the console. No exeception is thrown.
public static void main (String[] args) { String USR_HOME = System.getProperty("user.home"); String profileName = USR_HOME+"/.profile"; String installPath = System.getProperty("user.dir"); String str = ""; try { PrintWriter dynServ = new PrintWriter(new BufferedWriter(new FileWriter(profileName,true))); str = "FIC_HOME=" + installPath; dynServ.println(str); str = "export FIC_HOME"; dynServ.println(str); dynServ.flush(); dynServ.close(); }catch (Exception e){ System.out.println(e.getMessage()); } ProcessBuilder pb = new ProcessBuilder("sh","-c",". ./.profile"); Map<String, String> env = pb.environment(); env.put("FIC_HOME", USR_HOME+"/Path"); pb.directory(new File(USR_HOME)); try { Process p = pb.start(); BufferedReader Inreader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line=""; while ((line=Inreader.readLine())!=null) { System.out.println(line); //System.err.println(line); } } catch (Exception ex) { System.out.println("Exeception ::: "+ex.getMessage()); } System.out.println("FIC_HOME value :::"+System.getenv("FIC_HOME")); Output is: .profile executed FIC_HOME value :::null
Can anyone suggest something ??