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:
GeSHi (java):
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 {
String fNameOne =
"D:\\textfile\\";
//System.out.println("myDir:" +myDir);
File[] content=myDir.
listFiles(select
);
// System.out.println("dir length:" +chld.length );
for(int i = 0; i < chld.length; i++){
fileName = chld[i];
System.
out.
println("Filename:"+fileName
);
}
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
try {
// TODO Auto-generated catch block
// e.printStackTrace();
}
//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
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);
filepath=f.getParent();
System.
out.
println("filepath:" +filepath
);
// Transfer bytes from in to out
byte[] oBytes = new byte[1024];
int nLength;
while ((nLength = oBuffInputStream.read(oBytes)) > 0)
{
oOutStream.write(oBytes, 0, nLength);
}
oInStream.close();
oOutStream.close();
in.close();
// TODO Auto-generated catch block
e.printStackTrace();
}
// }
//Close the input stream
t1.sleep(1000);
System.
out.
println("FileName1111:" +fName
);
boolean s=ff.delete();
if (!s) {
System.
out.
println("File not deleted");
// File was not successfully moved
}
String fName1 =
"D:\\pitstop\\out";
//System.out.println("myDir:" +myDir);
File[] content1=myDir1.
listFiles(select1
);
System.
out.
println("chld4:" +chld4.
length);
if(chld4.length==0){
String fName2 =
"D:\\checkpdf\\pass\\";
//System.out.println("myDir:" +myDir);
File[] content2=myDir2.
listFiles(select2
);
// 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\\";
//System.out.println("myDir:" +myDir);
File[] content4=myDir31.
listFiles(select31
);
System.
out.
println("content4:" +content4
);
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\\";
//System.out.println("myDir:" +myDir);
File[] content5=myDir61.
listFiles(select41
);
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
// 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
// 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
);
// Destination directory
// 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 {
out.write("Invalid input file");
out.close();
}
}}
}
// }
}
automation1.main(args);
if(chld.length==0)
{
automation1.main(args);
}
}
}
Created by GeSHI 1.0.7.20
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.