title: java.lang.IllegalArgumentException when trying to read data from cookies in selenium webdriver
body: <p>I have written few lines of code which reads data from the cookies stored in a text file and then write it on the web browser whenever required.</p>
  
  <p>Block of code which i am using to store and write the cookies on a text file-</p>
  
  <pre><code>import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileWriter;
  
  import org.openqa.selenium.By;
  import org.openqa.selenium.Cookie;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  
  public class StoreCookieInfo {
  
  public static void main(String[] args) {
  System.setProperty("webdriver.chrome.driver","D:\\Java Programs and files\\chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  driver.get("http://www.facebook.com");
  driver.findElement(By.name("email")).sendKeys("Your username");
  driver.findElement(By.name("pass")).sendKeys("Your password");
  driver.findElement(By.name("persistent")).click();
  driver.findElement(By.name("pass")).submit();
  
  File f = new File("browser.data");
  try{
       f.delete();
       f.createNewFile();
       FileWriter fos = new FileWriter(f);
       BufferedWriter bos = new BufferedWriter(fos);
  
       for(Cookie ck : driver.manage().getCookies()) {
              bos.write((ck.getName()+";"+ck.getValue()+";"+ck.getDomain()
                      +";"+ck.getPath()+";"+ck.getExpiry()+";"+ck.isSecure()));
              bos.newLine();
       }
       bos.flush();
       bos.close();
       fos.close();
   }catch(Exception ex){
       ex.printStackTrace();
   }
  
    }
   }
  </code></pre>
  
  <p>It is running fine and storing data on text file.
  Block of code which i am using to read cookies from the text file-</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.util.Date;
  import java.util.StringTokenizer;
  
  import org.openqa.selenium.Cookie;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  
  public class LoadCookieInfo {
  
  @SuppressWarnings("deprecation")
  public static void main(String[] args){
      System.setProperty("webdriver.chrome.driver","D:\\Java Programs and files\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //WebDriver driver = new FirefoxDriver();
      driver.get("http://www.facebook.com");
      try{
           File f2 = new File("browser.data");
           FileReader fr = new FileReader(f2);
           BufferedReader br = new BufferedReader(fr);
           String line;
           while((line=br.readLine())!=null){
               StringTokenizer str = new StringTokenizer(line,";");
               while(str.hasMoreTokens()){
                   String name = str.nextToken();
                   String value = str.nextToken();
                   String domain = str.nextToken();
                   String path = str.nextToken();
                   System.out.println("1");
                   Date expiry = null;
                   String dt;
                   if(!(dt=str.nextToken()).equals("null")){
                       expiry = new Date(dt);
                   }
                   boolean isSecure = new Boolean(str.nextToken()).booleanValue();
                   Cookie ck = new Cookie(name,value,domain,path,expiry,isSecure);
                   driver.manage().addCookie(ck);
                   System.out.println(name+value);
               }
           }
      }catch(Exception ex){
           ex.printStackTrace();
      }
      driver.get("http://www.facebook.com");
   }
  }
  </code></pre>
  
  <p>When i am trying to run the second code i am getting the following exception on date -</p>
  
  <pre><code>java.lang.IllegalArgumentException
  at java.util.Date.parse(Unknown Source)
  at java.util.Date.&lt;init&gt;(Unknown Source)
  at com.Selenium_Practice.LoadCookieInfo.main(LoadCookieInfo.java:39)
  </code></pre>
  
  <p>This is the first time i am trying to read data using cookies so i am not able to figure out what wrong i am doing in the code.</p>