aspose file tools*
The moose likes Threads and Synchronization and the fly likes Process p .waitFor() waits for ever please give suggestions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Process p .waitFor() waits for ever please give suggestions" Watch "Process p .waitFor() waits for ever please give suggestions" New topic
Author

Process p .waitFor() waits for ever please give suggestions

Rakesh Shukla
Greenhorn

Joined: Jun 24, 2010
Posts: 2
Dear Members,

I am trying to run a perl script from java code that generates a log file on file system and populates the date in to mysql table.In java code while calling method Process p .waitFor(), the java program will hangs up and i am also using p.destroy() method but no further code will be executed.The process will wait forever. I have also seen that the perl file is being executed as it generates a log file with some data but it never completes I am using Windows Xp platform and jdk 1.5. But when i explicitly terminates the java program the log file will be generated immediately and data will be populated in mysql table.
My java program simple converts xls file to csv and then this csv file is passed as an argument for perl script file.

My java code is written below:


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;


public class ConvertXLSTOCSV
{
private static MyLogger lLog = MyLogger.getLogger();

public boolean convertXLSTOCSV(String xmlfilePath, String xlsFilename)
{
boolean conversionDoneFlag=false;
String csvFileName="";
String csvFilePath = "";
csvFilePath = xmlfilePath.substring(0, xmlfilePath.lastIndexOf("/"));
csvFilePath = csvFilePath+"/csv/";
lLog.info("csvFilePath :"+csvFilePath);

if(xlsFilename.contains(".xls")){
xlsFilename = xlsFilename.replaceAll(" ", "_");
csvFileName = xlsFilename.replaceAll(".xls", ".csv");
csvFileName = csvFilePath+csvFileName;
lLog.info("csvFileName : "+csvFileName);
xmlfilePath = xmlfilePath+"/"+xlsFilename;
lLog.info("xmlfilePath : "+xmlfilePath);

//File to store data in form of CSV
File f = new File(csvFileName);

OutputStream os = null;
try {
os = (OutputStream)new FileOutputStream(f);
} catch (FileNotFoundException e1) {
lLog.info("FileNotFoundException occurs");
e1.printStackTrace();
}
//String encoding = "UTF8";
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);

//Excel document to be imported
String filename = xmlfilePath;
lLog.info("XLS File path : "+filename);
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
Workbook w;
try {
w = Workbook.getWorkbook(new File(filename),ws);


// Gets the sheets from workbook
for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++)
{
Sheet s = w.getSheet(sheet);
Cell[] row = null;

// Gets the cells from sheet
// Gets the cells from sheet
for (int i = 0; i < s.getRows(); i++) {
row = s.getRow(i);
lLog.info("Row_no="+i+", row_length="+row.length);
// Fetching cell values
for (int j = 0 ; j < row.length; j++) {
String cellValue=row[j].getContents();
Pattern p = Pattern.compile("[\\n\\r\\t]");
Matcher m = p.matcher("");
m.reset(cellValue);
String result = m.replaceAll("");
if(j==13){
bw.write("\""+result+"\"");
lLog.info("Coloumn index ="+j+"and its value\""+result+"\"");
}else{
bw.write(result);
}
bw.write(',');
}

bw.newLine();
}
}

bw.flush();
bw.close();
lLog.info("Conversion completed");
} catch (BiffException e) {
lLog.info("BiffException occurs while Conversion");
e.printStackTrace();
}catch (UnsupportedEncodingException e){
System.err.println(e.toString());
}catch (IOException e){
System.err.println(e.toString());
}

// Call script to insert in temporary table
String pl_file = "";

lLog.info("going to execute perl file");
pl_file = "power_gen_report.pl";;
lLog.info("Perl file path :"+pl_file);

String cmdLine[] = { "perl", "-f", pl_file, csvFileName };
lLog.info("Perl command :"+"perl -f "+pl_file+" "+csvFileName );
try{
Runtime rt= Runtime.getRuntime();
Process p = rt.exec(cmdLine);
InputStream in = p.getInputStream();
InputStream err = p.getErrorStream();
boolean finished = false; // Set to true when p is finished
while( !finished) {
try {
while( in.available() > 0) {
// Print the output of our system call.
Character c = new Character( (char) in.read());
System.out.print(c);
}
while( err.available() > 0) {
// Print the output of errors
Character c = new Character( (char) in.read());
System.out.print(c);
}
int exitVal;
try {
exitVal = p.waitFor();
//int exitVal = p.exitValue();
finished = true;
System.out.println("finished = "+finished);

if(exitVal == 0){
lLog.info("perl file executed sucessfully :"+pl_file);
lLog.info("Table Populated sucessfully if csv file has new records\n");
conversionDoneFlag=true;
}else{
lLog.info("Table not Populated : Due to records in csv file are already present in table\n");
lLog.info("Exit Value returns from perl file : "+exitVal);

}
p.destroy();
} catch (InterruptedException e) {

e.printStackTrace();
}
} catch (IllegalThreadStateException e) {
// Sleep a little to save on CPU cycles
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
lLog.info("InterruptedException occurs ");
e1.printStackTrace();
}
}
}
}catch(IOException e){
lLog.info("IOException occurs ");
}

}

return conversionDoneFlag;
}

public static void main(String[] args){

System.out.println("Executing main() method ");
ConvertXLSTOCSV dv =new ConvertXLSTOCSV();
boolean ad = dv.convertXLSTOCSV("d:/attachment/excel","Technical_Associates_Ltd.16-06-2010.xls";);
if(ad== true){
System.out.println("Conversion completed");
}else{
System.out.println("Conversion not completed");
}
}

}

Please help me!.as i am unable to sort out this problem.
I will be very thankful to you for the same


Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
You must read from output and err streams of your process from separate threads to avoid system buffers overflow.
Read this article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=2
Rakesh Shukla
Greenhorn

Joined: Jun 24, 2010
Posts: 2
I have already read that but i never understood the reason foe hang up. Please do the need ful if any.
I will be vbery thanks ful to ypu for the same
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Process p .waitFor() waits for ever please give suggestions