在Java中捕获Perl异常
我一直在尝试通过Java运行Perl脚本。我甚至找到了如何执行的脚本,但问题是我希望Perl抛出的异常以Java返回在Java中捕获Perl异常,java,perl,Java,Perl,我一直在尝试通过Java运行Perl脚本。我甚至找到了如何执行的脚本,但问题是我希望Perl抛出的异常以Java返回 import java.io.IOException; public class Log { public static void main(String[] args) throws IOException { Process proc = null; try { proc = Runtime.g
import java.io.IOException;
public class Log {
public static void main(String[] args) throws IOException {
Process proc = null;
try
{
proc = Runtime.getRuntime().exec("perl C:\\Users\\myscript.pl");
int returncode = proc.exitValue();
System.out.println("Process Executed"+returncode);
}catch(Exception ae)
{
System.out.println("Exception is "+ae.toString());
}
finally
{
proc.destroy();
}
}
}
当我尝试运行此代码时,收到的异常是:
Exception is java.lang.IllegalThreadStateException: process has not exited
有人能帮我吗?我做错了什么?您需要在exitValue()之前调用Process.waitFor()。另外,我强烈推荐阅读,它有很多关于以更健壮的方式调用外部进程的有用信息
proc.waitFor(); // <======= blocks until process exit
int returncode = proc.exitValue();
System.out.println("Process Executed"+returncode)
proc.waitFor();// 您需要在exitValue()之前调用Process.waitFor()。另外,我强烈推荐阅读,它有很多关于以更健壮的方式调用外部进程的有用信息
proc.waitFor(); // <======= blocks until process exit
int returncode = proc.exitValue();
System.out.println("Process Executed"+returncode)
proc.waitFor();// 您需要在exitValue()之前调用Process.waitFor()。另外,我强烈推荐阅读,它有很多关于以更健壮的方式调用外部进程的有用信息
proc.waitFor(); // <======= blocks until process exit
int returncode = proc.exitValue();
System.out.println("Process Executed"+returncode)
proc.waitFor();// 您需要在exitValue()之前调用Process.waitFor()。另外,我强烈推荐阅读,它有很多关于以更健壮的方式调用外部进程的有用信息
proc.waitFor(); // <======= blocks until process exit
int returncode = proc.exitValue();
System.out.println("Process Executed"+returncode)
proc.waitFor();// 无论何时运行子进程,都需要确保以下两点:
a) 父进程应该监视子进程。
b) 父进程应等待子进程完成以检查其状态
为了实现第一个,您应该强制承诺同时使用子进程输出流和错误流。除此之外,父进程需要等待子进程完成其执行,然后才能获得其退出代码
要使用输出流和错误流,可以创建一个runnable,同时读取这两个流,并将输出记录到文件中
class StreamConsumer implements Runnable
{
private InputStream is;
private String outputFile;
StreamConsumer(InputStream is, String outputFile)
{
this.is = is;
this.outputFile = outputFile;
}
public void run()
{
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
BufferedWriter bufferedWriter = null;
char[] characterBuffer = null;
try
{
characterBuffer = new char[1024];
isr = new InputStreamReader(is,"UTF-8");
br = new BufferedReader(isr);
fos = new FileOutputStream(outputFile);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
LOGGER.info("Writing to op file: "+ this.outputFile);
int readCharacters = -1;
while ((readCharacters = br.read(characterBuffer,0,1024)) != -1)
{
String sw = new String(characterBuffer,0,readCharacters);
/*
* To ignore empty chunks
*/
if(sw != null && sw.trim().length() > 0)
{
bufferedWriter.write(sw.toString());
}
characterBuffer = new char[1024];
}
LOGGER.info("Done Writing to op file: "+ this.outputFile);
}
catch (Exception ioe)
{
LOGGER.error("IOException ", ioe);
}
finally
{
if(br != null)
{
try {
br.close();
} catch (IOException e) {
LOGGER.error("IOException ", e);
}
}
if(bufferedWriter != null)
{
try {
bufferedWriter.close();
} catch (IOException e) {
LOGGER.error("IOException ", e);
}
}
}
}
}
使用:
StreamConsumer errorStreamConsumer = new StreamConsumer(proc.getErrorStream(),"errorfilepath");
StreamConsumer outputStreamConsumer = new StreamConsumer(proc.getInputStream(),"outputFilepath");
执行上述操作将确保子进程从不挂起,并且由父进程有效地监视
另请参见:
要实现第二个,父进程需要等待子进程完成其执行:
proc.waitFor();
如有必要,使当前线程等待进程结束
由此进程对象表示的已终止。此方法返回
如果子流程已终止,则立即执行。如果
子进程尚未终止,调用线程将被阻止
直到子流程退出
在请求子进程返回代码之前,这是必需的
int returncode = proc.exitValue();
无论何时运行子进程,都需要确保以下两点:
a) 父进程应该监视子进程。
b) 父进程应等待子进程完成以检查其状态
为了实现第一个,您应该强制承诺同时使用子进程输出流和错误流。除此之外,父进程需要等待子进程完成其执行,然后才能获得其退出代码
要使用输出流和错误流,可以创建一个runnable,同时读取这两个流,并将输出记录到文件中
class StreamConsumer implements Runnable
{
private InputStream is;
private String outputFile;
StreamConsumer(InputStream is, String outputFile)
{
this.is = is;
this.outputFile = outputFile;
}
public void run()
{
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
BufferedWriter bufferedWriter = null;
char[] characterBuffer = null;
try
{
characterBuffer = new char[1024];
isr = new InputStreamReader(is,"UTF-8");
br = new BufferedReader(isr);
fos = new FileOutputStream(outputFile);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
LOGGER.info("Writing to op file: "+ this.outputFile);
int readCharacters = -1;
while ((readCharacters = br.read(characterBuffer,0,1024)) != -1)
{
String sw = new String(characterBuffer,0,readCharacters);
/*
* To ignore empty chunks
*/
if(sw != null && sw.trim().length() > 0)
{
bufferedWriter.write(sw.toString());
}
characterBuffer = new char[1024];
}
LOGGER.info("Done Writing to op file: "+ this.outputFile);
}
catch (Exception ioe)
{
LOGGER.error("IOException ", ioe);
}
finally
{
if(br != null)
{
try {
br.close();
} catch (IOException e) {
LOGGER.error("IOException ", e);
}
}
if(bufferedWriter != null)
{
try {
bufferedWriter.close();
} catch (IOException e) {
LOGGER.error("IOException ", e);
}
}
}
}
}
使用:
StreamConsumer errorStreamConsumer = new StreamConsumer(proc.getErrorStream(),"errorfilepath");
StreamConsumer outputStreamConsumer = new StreamConsumer(proc.getInputStream(),"outputFilepath");
执行上述操作将确保子进程从不挂起,并且由父进程有效地监视
另请参见:
要实现第二个,父进程需要等待子进程完成其执行:
proc.waitFor();
如有必要,使当前线程等待进程结束
由此进程对象表示的已终止。此方法返回
如果子流程已终止,则立即执行。如果
子进程尚未终止,调用线程将被阻止
直到子流程退出
在请求子进程返回代码之前,这是必需的
int returncode = proc.exitValue();
无论何时运行子进程,都需要确保以下两点:
a) 父进程应该监视子进程。
b) 父进程应等待子进程完成以检查其状态
为了实现第一个,您应该强制承诺同时使用子进程输出流和错误流。除此之外,父进程需要等待子进程完成其执行,然后才能获得其退出代码
要使用输出流和错误流,可以创建一个runnable,同时读取这两个流,并将输出记录到文件中
class StreamConsumer implements Runnable
{
private InputStream is;
private String outputFile;
StreamConsumer(InputStream is, String outputFile)
{
this.is = is;
this.outputFile = outputFile;
}
public void run()
{
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
BufferedWriter bufferedWriter = null;
char[] characterBuffer = null;
try
{
characterBuffer = new char[1024];
isr = new InputStreamReader(is,"UTF-8");
br = new BufferedReader(isr);
fos = new FileOutputStream(outputFile);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
LOGGER.info("Writing to op file: "+ this.outputFile);
int readCharacters = -1;
while ((readCharacters = br.read(characterBuffer,0,1024)) != -1)
{
String sw = new String(characterBuffer,0,readCharacters);
/*
* To ignore empty chunks
*/
if(sw != null && sw.trim().length() > 0)
{
bufferedWriter.write(sw.toString());
}
characterBuffer = new char[1024];
}
LOGGER.info("Done Writing to op file: "+ this.outputFile);
}
catch (Exception ioe)
{
LOGGER.error("IOException ", ioe);
}
finally
{
if(br != null)
{
try {
br.close();
} catch (IOException e) {
LOGGER.error("IOException ", e);
}
}
if(bufferedWriter != null)
{
try {
bufferedWriter.close();
} catch (IOException e) {
LOGGER.error("IOException ", e);
}
}
}
}
}
使用:
StreamConsumer errorStreamConsumer = new StreamConsumer(proc.getErrorStream(),"errorfilepath");
StreamConsumer outputStreamConsumer = new StreamConsumer(proc.getInputStream(),"outputFilepath");
执行上述操作将确保子进程从不挂起,并且由父进程有效地监视
另请参见:
要实现第二个,父进程需要等待子进程完成其执行:
proc.waitFor();
如有必要,使当前线程等待进程结束
由此进程对象表示的已终止。此方法返回
如果子流程已终止,则立即执行。如果
子进程尚未终止,调用线程将被阻止
直到子流程退出
在请求子进程返回代码之前,这是必需的
int returncode = proc.exitValue();
无论何时运行子进程,都需要确保以下两点:
a) 父进程应该监视子进程。
b) 父进程应等待子进程完成以检查其状态
为了实现第一个,您应该强制承诺同时使用子进程输出流和错误流。除此之外,父进程需要等待子进程完成其执行,然后才能获得其退出代码
要使用输出流和错误流,可以创建一个runnable,同时读取这两个流,并将输出记录到文件中
class StreamConsumer implements Runnable
{
private InputStream is;
private String outputFile;
StreamConsumer(InputStream is, String outputFile)
{
this.is = is;
this.outputFile = outputFile;
}
public void run()
{
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
BufferedWriter bufferedWriter = null;
char[] characterBuffer = null;
try
{
characterBuffer = new char[1024];
isr = new InputStreamReader(is,"UTF-8");
br = new BufferedReader(isr);
fos = new FileOutputStream(outputFile);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
LOGGER.info("Writing to op file: "+ this.outputFile);
int readCharacters = -1;
while ((readCharacters = br.read(characterBuffer,0,1024)) != -1)
{
String sw = new String(characterBuffer,0,readCharacters);
/*
* To ignore empty chunks
*/
if(sw != null && sw.trim().length() > 0)
{
bufferedWriter.write(sw.toString());
}
characterBuffer = new char[1024];
}
LOGGER.info("Done Writing to op file: "+ this.outputFile);
}
catch (Exception ioe)
{
LOGGER.error("IOException ", ioe);
}
finally
{
if(br != null)
{
try {
br.close();
} catch (IOException e) {
LOGGER.error("IOException ", e);
}
}
if(bufferedWriter != null)
{
try {
bufferedWriter.close();
} catch (IOException e) {
LOGGER.error("IOException ", e);
}
}
}
}
}
使用:
StreamConsumer errorStreamConsumer = new StreamConsumer(proc.getErrorStream(),"errorfilepath");
StreamConsumer outputStreamConsumer = new StreamConsumer(proc.getInputStream(),"outputFilepath");
执行上述操作将确保子进程从不挂起,并且由