title: Giving File permissions and running ProcessBuilder in Java Servlets
body: <p>I have tried coding a servlet which creates a file based on the inputs given to it.The file gets created along with appropriate text but I am unable to give the file 777 permissions and I am also unable to run ProcessBuilder later.
  I thought it is inter related because the command fired in ProcessBuilder would require the file to have appropriate permissions but when I try giving it permissions using chmod it doesn't work either. </p>
  
  <pre><code>import java.io.*;
  import java.util.Enumeration;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.lang.*;
  import java.io.*;
  import java.util.*;
  public class Serv extends HttpServlet
  { 
      private static final long serialVersionUID = 1L;
      public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
      {
      String jobId=req.getParameter("jobId");
      String jobStatus=req.getParameter("jobStatus");
      String displayName=req.getParameter("displayName");
      String name=req.getParameter("name");
      String description=req.getParameter("description");
      String frequency=req.getParameter("frequency");
      String lastModifiedAt=req.getParameter("lastModifiedAt");
      String createdAt=req.getParameter("createdAt"); 
      String createdBy=req.getParameter("createdBy");     
      String opPath=req.getParameter("opPath");       
      String env=req.getParameter("env");     
      res.setContentType("text/html");
      PrintWriter pw=res.getWriter();
      pw.println("JobId: \n"+jobId);
      pw.println("JobStatus: \n"+jobStatus);
      String path = getServletContext().getRealPath("/");
      File logfile=new File("/var/lib/tomcat7/webapps/ROOT/log.txt");
      logfile.setReadable(true);
      logfile.setWritable(true);
      logfile.setExecutable(true);    
      PrintWriter writer = new PrintWriter(path+"log.txt", "UTF-8");
      writer.println("Job ID : "+jobId);
      writer.println("Job Status : "+jobStatus);
      writer.println("Rule Name : "+name);
      writer.println("Rule Display Name : "+displayName);
      writer.println("Rule Description : "+description);
      writer.println("Rule env : "+env);
      writer.println("Rule frequency : "+frequency);
      writer.println("Rule last modified at : "+lastModifiedAt);
      writer.println("Rule created at : "+createdAt);
      writer.println("Rule created by : "+createdBy);
      writer.println("Notification Path : "+opPath);
      writer.close();
      pw.close();
      try
          {
          // ProcessBuilder pb1=new ProcessBuilder("sudo","/bin/chmod","777","/var/lib/tomcat7/webapps/ROOT/log.txt");
          // pb1.redirectErrorStream(true);
          // Process p1=pb1.start();
          // InputStreamReader isr1 = new  InputStreamReader(p1.getInputStream());
          // BufferedReader br1 = new BufferedReader(isr1);
          // String lineRead1;
          // while ((lineRead1 = br1.readLine()) != null) 
          //     {
          //  System.out.println(lineRead1);
          //     }
          // p1.waitFor();
          ProcessBuilder pb2=new ProcessBuilder("/usr/local/hadoop/bin/hadoop", "fs", "-copyFromLocal", "/var/lib/tomcat7/webapps/ROOT/log.txt",opPath);
          pb2.redirectErrorStream(true);
      //  pb2.directory(new File("/var/lib/tomcat7/webapps/ROOT/"));
          Process p2=pb2.start();
          InputStreamReader isr2 = new  InputStreamReader(p2.getInputStream());
          BufferedReader br2 = new BufferedReader(isr2);
          String lineRead2;
          while ((lineRead2 = br2.readLine()) != null) 
              {
              System.out.println(lineRead2);
              }
          p2.waitFor();
          }
      catch(Exception e){}
  
      }
      public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
      {
      String jobId=req.getParameter("jobId");
      String jobStatus=req.getParameter("jobStatus");
      String displayName=req.getParameter("displayName");
      String name=req.getParameter("name");
      String description=req.getParameter("description");
      String frequency=req.getParameter("frequency");
      String lastModifiedAt=req.getParameter("lastModifiedAt");
      String createdAt=req.getParameter("createdAt"); 
      String createdBy=req.getParameter("createdBy");     
      String env=req.getParameter("env");     
      String opPath=req.getParameter("opPath");       
      res.setContentType("text/html");
      PrintWriter pw=res.getWriter();
      pw.println("JobId: \n"+jobId);
      pw.println("JobStatus: \n"+jobStatus);
      String path = getServletContext().getRealPath("/");
      File logfile=new File("/var/lib/tomcat7/webapps/ROOT/log.txt");
      logfile.setReadable(true);
      logfile.setWritable(true);
      logfile.setExecutable(true);    
      PrintWriter writer = new PrintWriter(path+"log.txt", "UTF-8");
      writer.println("Job ID : "+jobId);
      writer.println("Job Status : "+jobStatus);
      writer.println("Rule Name : "+name);
      writer.println("Rule Display Name : "+displayName);
      writer.println("Rule Description : "+description);
      writer.println("Rule env : "+env);
      writer.println("Rule frequency : "+frequency);
      writer.println("Rule last modified at : "+lastModifiedAt);
      writer.println("Rule created at : "+createdAt);
      writer.println("Rule created by : "+createdBy);
      writer.println("Notification Path : "+opPath);
      writer.close();
          pw.close();
          try
          {
          // ProcessBuilder pb1=new ProcessBuilder("sudo", "/bin/chmod","777", "-R", "/var/lib/tomcat7/webapps/ROOT/log.txt");
          // pb1.redirectErrorStream(true);
          // Process p1=pb1.start();
          // InputStreamReader isr1 = new  InputStreamReader(p1.getInputStream());
          // BufferedReader br1 = new BufferedReader(isr1);
          // String lineRead1;
          // while ((lineRead1 = br1.readLine()) != null) 
          //     {
          //  System.out.println(lineRead1);
          //     }
          // p1.waitFor();
          ProcessBuilder pb2=new ProcessBuilder("/usr/local/hadoop/bin/hadoop", "fs", "-copyFromLocal", "/var/lib/tomcat7/webapps/ROOT/log.txt",opPath);
          pb2.redirectErrorStream(true);
      //  pb2.directory(new File("/var/lib/tomcat7/webapps/ROOT/"));
          Process p2=pb2.start();
          InputStreamReader isr2 = new  InputStreamReader(p2.getInputStream());
          BufferedReader br2 = new BufferedReader(isr2);
          String lineRead2;
          while ((lineRead2 = br2.readLine()) != null) 
              {
              System.out.println(lineRead2);
              }
          p2.waitFor();
          }
      catch(Exception e){}
      }
  }
  </code></pre>
  
  <p>It would be really great if anyone could help.</p>
  
  <p>For Convenience :
  File Permissions</p>
  
  <pre><code>File logfile=new File("/var/lib/tomcat7/webapps/ROOT/log.txt");
  logfile.setReadable(true);
  logfile.setWritable(true);
  logfile.setExecutable(true);    
  </code></pre>
  
  <p>ProcessBuilder :</p>
  
  <pre><code>ProcessBuilder pb2=new ProcessBuilder("/usr/local/hadoop/bin/hadoop", "fs", "-copyFromLocal", "/var/lib/tomcat7/webapps/ROOT/log.txt",opPath);
          pb2.redirectErrorStream(true);
      //  pb2.directory(new File("/var/lib/tomcat7/webapps/ROOT/"));
          Process p2=pb2.start();
          InputStreamReader isr2 = new  InputStreamReader(p2.getInputStream());
          BufferedReader br2 = new BufferedReader(isr2);
          String lineRead2;
          while ((lineRead2 = br2.readLine()) != null) 
              {
              System.out.println(lineRead2);
              }
          p2.waitFor();
  </code></pre>