Hi all,

I have a java program which runs repeatedly by calling to main functions in a loop based on a condition ..But after running for some time(say 30 minutes),it shows the follwing error and it terminate the program execution.But as per my requirement i want to run this program for several hours or time without showing any error and terminating the program.

Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.StreamEncoder$CharsetSE.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at pitstop.automation1.main(automation1.java:18)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)


Could anyone please tell me what is the exact reason for this and how to fix this error.

The code for the same program is below:

package pitstop;   
  
import java.io.*;     
import java.util.*;     
import java.io.Closeable;   
import java.io.DataInputStream;   
import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.FilenameFilter;   
import java.io.IOException;   
  
  
public class automation1 {   
           
    public static void main(String[] args) throws InterruptedException {     
           
        System.out.println("--");   
         String str3=null;   
        String fNameOne = "D:\\textfile\\";   
        File myDir=new File(fNameOne);   
        //System.out.println("myDir:" +myDir);   
        FilenameFilter select=new FileListFilter("","txt");   
        File[] content=myDir.listFiles(select);   
        String  filepath=null;   
        String fName=null;   
        String fileName = null;   
        String fileName1=null;   
        File dir = new File(fNameOne);   
        String[] chld = dir.list();   
       // System.out.println("dir length:" +chld.length );   
        for(int i = 0; i < chld.length; i++){   
             fileName = chld[i];   
            System.out.println("Filename:"+fileName);   
        }   
                 
        Thread t=new Thread();   
           
        t.sleep(1000);   
       
           
        //if(content!=null){   
        for(int i = 0; i < chld.length; i++){   
               
             fileName1 = chld[i];   
             System.out.println("Fname:"+fNameOne);   
             System.out.println("Filename1:"+fileName1);   
          //  switch(1){   
           // case 1:{   
                   
                               
                   
                fName = fNameOne+fileName1;   
                System.out.println("Fname1:" +fName);   
                String thisLine; //string variable which take each line at a time     
                FileInputStream fis = null;     
                   
                 try {    
                           
                        fis = new FileInputStream(fName);     
                           
                    } catch (FileNotFoundException e) {     
                        // TODO Auto-generated catch block     
                      //  e.printStackTrace();     
                    }   
                   
  
                DataInputStream in = new DataInputStream(fis);   
                BufferedReader br = new BufferedReader(new InputStreamReader(in));   
                String strLine = null;   
                String replacewith="\\";   
                String strLine1=null;   
                  
                //Read File Line By Line   
               // while (strLine  != null)   {   
                try {   
                    while ((strLine = br.readLine()) != null)   {   
  
                    //while ((thisLine = in.readLine()) != null)     
                      // Print the content on the console   
                      System.out.println (strLine);   
                      String str=strLine.substring(0,1);   
                      String str1=strLine.replaceFirst(str,"" );   
                      String str2=str1.replaceFirst("/", ":\\\\");   
                       str3=str2.replaceAll("/","\\\\");   
                      System.out.println ("str11:" +str3);   
                    /*  strLine=strLine.replaceAll("/d","D:");  
                      System.out.println ("str:" +strLine);  
                     strLine1=strLine.replaceAll("/","\\\\" );  
                     System.out.println ("str11:" +strLine1);  
                     */    
                    }   
                //  System.out.println("str111" +strLine1);   
                    File f=new File(str3);   
                    String filename=f.getName();   
                    filepath=f.getParent();   
                    System.out.println("filepath:" +filepath);   
                    InputStream oInStream = new FileInputStream(str3);   
                    OutputStream oOutStream = new FileOutputStream("D:\\pitstop\\in\\" +filename);   
  
                    // Transfer bytes from in to out   
                    byte[] oBytes = new byte[1024];   
                    int nLength;   
                    BufferedInputStream oBuffInputStream = new BufferedInputStream( oInStream );   
                    while ((nLength = oBuffInputStream.read(oBytes)) > 0)    
                    {   
                            oOutStream.write(oBytes, 0, nLength);   
                    }   
                    oInStream.close();   
                    oOutStream.close();   
                       
                    in.close();   
                    } catch (IOException e) {   
                    // TODO Auto-generated catch block   
                    e.printStackTrace();   
                }   
              //  }   
                //Close the input stream   
                    Thread t1=new Thread();   
                       
                    t1.sleep(1000);   
                       
                    System.out.println("FileName1111:" +fName);   
                    File ff=new File(fName);   
                    System.out.println("ff" +ff);   
                     boolean s=ff.delete();   
                     System.out.println("s" +s);   
                     if (!s) {   
                            System.out.println("File not deleted");   
                            // File was not successfully moved   
                        }              
                       
                       
            String fName1 = "D:\\pitstop\\out";   
            File myDir1=new File(fName1);   
            //System.out.println("myDir:" +myDir);   
            FilenameFilter select1=new FileListFilter("","pdf");   
            File[] content1=myDir1.listFiles(select1);   
               
            File dir4 = new File(fName1);   
            String[] chld4 = dir4.list();   
            System.out.println("chld4:" +chld4.length);   
               
               
               
            if(chld4.length==0){   
                                   
                String fName2 = "D:\\checkpdf\\pass\\";   
                File myDir2=new File(fName2);   
                //System.out.println("myDir:" +myDir);   
                FilenameFilter select2=new FileListFilter("","pdf");   
                File[] content2=myDir2.listFiles(select2);   
                   
  
                String fileName3 = null;   
                File dir3 = new File(fName2);   
                String[] chld1 = dir3.list();   
               // System.out.println("dir length:" +chld.length );   
                for(int j = 0; j < chld1.length; j++){   
                     fileName3 = chld1[j];   
                   // System.out.println("Filename3:" +fileName3);   
                }   
               // System.out.println("content2:" +content2);   
                   
                String fName31 = "D:\\checkpdf\\fail\\";   
                File myDir31=new File(fName31);   
                //System.out.println("myDir:" +myDir);   
                FilenameFilter select31=new FileListFilter("","pdf");   
                File[] content4=myDir31.listFiles(select31);   
                System.out.println("content4:" +content4);   
  
                String fileName4 = null;   
                File dir51 = new File(fName31);   
                String[] chld2 = dir51.list();   
               // System.out.println("dir length:" +chld.length );   
                for(int k = 0; k < chld2.length; k++){   
                     fileName4 = chld2[k];   
                  // System.out.println("Filename in fail:" +fileName4);   
                }   
                   
                String fName5 = "D:\\checkpdf\\corrupt\\";   
                File myDir61=new File(fName5);   
                //System.out.println("myDir:" +myDir);   
                FilenameFilter select41=new FileListFilter("","pdf");   
                File[] content5=myDir61.listFiles(select41);   
                   
  
                String fileName5 = null;   
                File dir61 = new File(fName5);   
                String[] chld5 = dir61.list();   
               // System.out.println("dir length:" +chld.length );   
                for(int l = 0; l < chld5.length; l++){   
                     fileName5 = chld5[l];   
                   // System.out.println("Filename:"+fileName);   
                }   
                   
                   
                   
                if(content2!=null){   
                       
                    System.out.println("in pass");   
                       
                    for(int a=0;a<chld1.length;a++){   
                       
                        fileName3 = chld1[a];   
                     File file1 = new File("D:\\checkpdf\\pass\\" +fileName3 );   
                        
                     System.out.println("File name in pass:" +fileName3);   
                        
                           
                        // Destination directory   
                        File dir1 = new File(filepath);   
                           
                        // Move file to new directory   
                        boolean success = file1.renameTo(new File(dir1, file1.getName()));   
                        if (!success) {   
                            System.out.println("File not moved from pass");   
                            // File was not successfully moved   
                        }   
                        else  
                            System.out.println("File moved from pass");   
                    }   
                   
                   
                }   
                   
                   
                 if(content4!=null){   
                        
                    System.out.println("in fail");   
                     File file2 = new File("D:\\checkpdf\\fail\\" +fileName4 );   
                        
                        
                     System.out.println("File name in fail:" +fileName3);   
                       
                    // Destination directory   
                    File dir7 = new File(filepath);   
                       
                    // Move file to new directory   
                    boolean success11 = file2.renameTo(new File(dir7, file2.getName()));   
                    if (!success11) {   
                           
                        // File was not successfully moved   
                        System.out.println("File not moved from fail");   
                    }   
                   else  
                        System.out.println("File moved from fail");   
                       
                       
                      
  
                       
                       
                       
                       
                }   
                   
                if(content5!=null){   
                       
                     System.out.println("in corrupt");   
                     File file3 = new File("D:\\checkpdf\\corrupt\\" +fileName5 );   
                        
                     String readme="readme.txt";   
                        
                       
                    // Destination directory   
                    File dir8 = new File(filepath);   
                       
                    // Move file to new directory   
                    boolean success111 = file3.renameTo(new File(dir8, file3.getName()));   
                    if (!success111) {   
                        System.out.println("File not moved from corrupt");   
                        // File was not successfully moved   
                    }else{   
                        System.out.println("File moved from corrupt");   
                       
                       
                  try {   
                    BufferedWriter out = new BufferedWriter(new FileWriter(filepath +"\\" +readme));   
                    out.write("Invalid input file");   
                    out.close();   
                } catch (IOException e) {   
                }   
                       
                       
                }}   
                   
            }   
                 
                       
        //    }   
              
               
                   
  
               
               
          }   
        automation1.main(args);   
            
               
        if(chld.length==0)   
        {   
            automation1.main(args);   
               
        }   
            }   
    }

Here i am calling the main function again and again using automation1.main(args) at line no 299 and 304.

Please help me in this issue.


Thanks,
Sanat

Recommended Answers

All 3 Replies

what are you trying to accomplish?
Beacause when a main calls a main method, the second main will again call the main method, which will again call the main method, which will again call the main method, which will again call the main method,
OH NO, the stack is full, you have too many mains running; I can't take it anymore.
So naturally you get an error.
I haven't read the code, better explain it first. Maybe there is another way to do it

Also check your base conditions. I can see two calls to the main().
I dont think the first call is inside a base condition and that cud be why the stack overflows.
Please chk ur code properly.

Recursive functions uses STACK frame and for an instance of JVM, it is 1 MB. Please try loop control statements instead of recursion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.