Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java error "java.lang.StackOverflowError"

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java error "java.lang.StackOverflowError"

    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(Unkno wn 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.

    regards,
    sanat
    Last edited by sanatkumar; April 30th, 2009 at 01:27 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: how to solve this type of stackoverflowerror...

    Hello sanatkumar and welcome to the Java Programming Forums

    Can you please tell me what is the purpose of this application?

    The error you are getting is thrown when a stack overflow occurs because an application recurses too deeply.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: how to solve this type of stackoverflowerror...

    I would say this is happening because you are calling your main method from within your main method hence a stack overflow.

    Try removing the last couple of lines of code.

    automation1.main(args);
     
     
        	if(chld.length==0)
        	{
        		automation1.main(args);
     
        	}

    // Json

Similar Threads

  1. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 02:41 AM
  2. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM
  3. Replies: 2
    Last Post: May 16th, 2009, 05:23 AM
  4. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 03:11 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 02:18 PM