//output information to text file. //I don't know why information will generated through ErrorStream, as the // subprocess executed sucessfully. Any Explaination? // Anyway following code works.public static void main(String args[]){ try{ Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("javac -verbose Test.java");
InputStream stdin = proc.getErrorStream(); //I don't why only ErrorStream InputStreamReader isr = new InputStreamReader(stdin); BufferedReader br = new BufferedReader(isr); FileWriter wr =new FileWriter(new File("1.txt")); System.out.println("<OUTPUT to 1.txt>"); String line; while ( (line = br.readLine()) != null) wr.write(line+"\r\n"); wr.flush(); System.out.println("</OUTPUT>"); //close stream .... int exitVal = proc.waitFor(); System.out.println("Process exitValue: " + exitVal); } catch (Throwable t){ t.printStackTrace(); } }
我是把要执行的命令写入一个批处理文件,然后构建一个类,在类中执行这个批处理文件并接收输出,用vb作的,挺简单的,有一点vb的基础就应该能看懂下面这个函数,这是类中的主要的函数,基本上只要这个函数就可以了 Public Function ExecuteCommand(Optional CommandLine As String) As String Dim proc As PROCESS_INFORMATION 'Process info filled by CreateProcessA Dim ret As Long 'long variable for get the return value of the 'API functions Dim start As STARTUPINFO 'StartUp Info passed to the CreateProceeeA 'function Dim sa As SECURITY_ATTRIBUTES 'Security Attributes passeed to the 'CreateProcessA function Dim hReadPipe As Long 'Read Pipe handle created by CreatePipe Dim hWritePipe As Long 'Write Pite handle created by CreatePipe Dim lngBytesread As Long 'Amount of byte read from the Read Pipe handle Dim strBuff As String * 256 'String buffer reading the Pipe 'if the parameter is not empty update the CommandLine property If Len(CommandLine) > 0 Then mCommand = CommandLine End If
'if the command line is empty then exit whit a error message If Len(mCommand) = 0 Then MsgBox "Command Line empty", vbCritical Exit Function End If
'Create the Pipe sa.nLength = Len(sa) sa.bInheritHandle = 1& sa.lpSecurityDescriptor = 0& ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then 'If an error occur during the Pipe creation exit MsgBox "CreatePipe failed. Error: " & Err.LastDllError, vbCritical Exit Function End If
'Launch the command line application start.cb = Len(start) start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW 'set the StdOutput and the StdError output to the same Write Pipe handle start.hStdOutput = hWritePipe start.hStdError = hWritePipe 'Execute the command ret& = CreateProcessA(0&, mCommand, sa, sa, 1&, _ NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
If ret <> 1 Then 'if the command is not found .... MsgBox "File or command not found", vbCritical Exit Function End If
'Now We can ... must close the hWritePipe ret = CloseHandle(hWritePipe) mOutputs = ""
'Read the ReadPipe handle Do ret = ReadFile(hReadPipe, strBuff, 256, lngBytesread, 0&) mOutputs = mOutputs & Left(strBuff, lngBytesread) 'Send data to the object via ReceiveOutputs event RaiseEvent ReceiveOutputs(Left(strBuff, lngBytesread)) Loop While ret <> 0
'Close the opened handles ret = CloseHandle(proc.hProcess) ret = CloseHandle(proc.hThread) ret = CloseHandle(hReadPipe) If mOutputs <> "" Then blnCompiledSuccessful = False Open strRoot & "BatchBuild.log" For Append As #3 Print #3, "" Print #3, mOutputs 'Print #3, "" Print #3, "-------------------------------------------------------------------------------------------------" Close #3 Else blnCompiledSuccessful = True Open strRoot & "BatchBuild.log" For Append As #3 Print #3, "" Print #3, strTargetFile & " 编译成功" Print #3, "" Print #3, "-------------------------------------------------------------------------------------------------" Close #3 End If 'Return the Outputs property with the entire DOS output ExecuteCommand = mOutputs End Function
import java.io.*;public class JavaCP
{
public static void main(String[] args)
{
try
{
Runtime rt=Runtime.getRuntime(); //Process pr=rt.exec("C:\\j2sdk1403\\bin\\javac.exe -classpath .;d:\\Java; TestJavaCP.java");//指明绝对路径
Process pr=rt.exec("java TestJavaCP");//与上面效果一样
//Process pr=rt.exec("javac -classpath .;d:\\Java; TestJavaCP.java");//java文件必须放置在当前目录。
//Process pr=rt.exec("java -version"); //pr.waitFor();//如想得到输出结果,最好不要用该方法。 //OutputStream getOutputStream()
//InputStream is=pr.getErrorStream();
InputStream is=pr.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String tmp=null;
while((tmp=br.readLine())!=null)
{
System.out.println(tmp);
}
}
catch(Exception err)
{
System.out.println(err.getMessage());
}
}
}
但用在
别的程序好使
是指在运行javac时在控制台下打印的报错信息吗?
看到的信息
最好用java和delphi都能实现
看来只有等着Leemaasn(我给大家拜早年啦!新春快乐!!!)大哥解决了 ^_^
想办法把dos窗口的所有字符都输出到文件;
(这个用手动复制粘贴我想大家都会,实现模拟鼠标键盘的操作我想也不会太难)
然后你再对这个文件操作,去掉没用的信息,不就行了?
谢谢
但我的目的是编译信息
还要请大家继续想办法
//I don't know why information will generated through ErrorStream, as the
// subprocess executed sucessfully. Any Explaination?
// Anyway following code works.public static void main(String args[]){
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac -verbose Test.java");
InputStream stdin = proc.getErrorStream(); //I don't why only ErrorStream
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
FileWriter wr =new FileWriter(new File("1.txt"));
System.out.println("<OUTPUT to 1.txt>");
String line;
while ( (line = br.readLine()) != null)
wr.write(line+"\r\n");
wr.flush();
System.out.println("</OUTPUT>");
//close stream ....
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t){
t.printStackTrace();
}
}
信息当然可以保存到.txt文件里面阿 重定向
{
public static void main(String[] args)
{
System.djkf;
}
}
这个文件是有错误的,你在dos下面用javac TestError.java 2> error.txt编译的时候
控制台并不显示错误 但是在这个目录下就会有一个error.txt文件 内容是
TestError.java:5: not a statement
System.djkf;
^
1 error
这个前提是你得系统为NT/2000/xp
如果你得系统是85/98的话不支持重定向功能,要么省级操作系统 要么就下一个errout程序
errout javac TestError.java > error.txt
建议省级操作系统 98不行 烂
说的好像对大家帮着看看为什莫重定向加个2你在dos下面用javac TestError.java 2> error.txt编译
仅仅指个方向,不会认为你是正确的象两位高手一样的高手
如果用更多的时间去指方向
而不是详细的编码,将做多么多有意义的事呜呼,唉哉~~~CSDN不开高级版,高手们何去何从??
InputStream stdin = proc.getErrorStream(); //I don't why only ErrorStream是取得错误信息的关键
给分了
我是在别的程序中通过创建管道(pipe)实现输出重定向来获得编译后的输出信息的
来晚了,其实我对java并不是很熟,只不过为了响应Leemaasn兄弟的需要,写过一个方便编译的小工具,才想办法实现这个功能的
Public Function ExecuteCommand(Optional CommandLine As String) As String
Dim proc As PROCESS_INFORMATION 'Process info filled by CreateProcessA
Dim ret As Long 'long variable for get the return value of the
'API functions
Dim start As STARTUPINFO 'StartUp Info passed to the CreateProceeeA
'function
Dim sa As SECURITY_ATTRIBUTES 'Security Attributes passeed to the
'CreateProcessA function
Dim hReadPipe As Long 'Read Pipe handle created by CreatePipe
Dim hWritePipe As Long 'Write Pite handle created by CreatePipe
Dim lngBytesread As Long 'Amount of byte read from the Read Pipe handle
Dim strBuff As String * 256 'String buffer reading the Pipe 'if the parameter is not empty update the CommandLine property
If Len(CommandLine) > 0 Then
mCommand = CommandLine
End If
'if the command line is empty then exit whit a error message
If Len(mCommand) = 0 Then
MsgBox "Command Line empty", vbCritical
Exit Function
End If
'Create the Pipe
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then
'If an error occur during the Pipe creation exit
MsgBox "CreatePipe failed. Error: " & Err.LastDllError, vbCritical
Exit Function
End If
'Launch the command line application
start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
'set the StdOutput and the StdError output to the same Write Pipe handle
start.hStdOutput = hWritePipe
start.hStdError = hWritePipe
'Execute the command
ret& = CreateProcessA(0&, mCommand, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
If ret <> 1 Then
'if the command is not found ....
MsgBox "File or command not found", vbCritical
Exit Function
End If
'Now We can ... must close the hWritePipe
ret = CloseHandle(hWritePipe)
mOutputs = ""
'Read the ReadPipe handle
Do
ret = ReadFile(hReadPipe, strBuff, 256, lngBytesread, 0&)
mOutputs = mOutputs & Left(strBuff, lngBytesread)
'Send data to the object via ReceiveOutputs event
RaiseEvent ReceiveOutputs(Left(strBuff, lngBytesread))
Loop While ret <> 0
'Close the opened handles
ret = CloseHandle(proc.hProcess)
ret = CloseHandle(proc.hThread)
ret = CloseHandle(hReadPipe)
If mOutputs <> "" Then
blnCompiledSuccessful = False
Open strRoot & "BatchBuild.log" For Append As #3
Print #3, ""
Print #3, mOutputs
'Print #3, ""
Print #3, "-------------------------------------------------------------------------------------------------"
Close #3
Else
blnCompiledSuccessful = True
Open strRoot & "BatchBuild.log" For Append As #3
Print #3, ""
Print #3, strTargetFile & " 编译成功"
Print #3, ""
Print #3, "-------------------------------------------------------------------------------------------------"
Close #3
End If
'Return the Outputs property with the entire DOS output
ExecuteCommand = mOutputs
End Function
aikill(大傻)果然够兄弟!!!!!!!!!!!!!!!!!!!!!!!!
楼主看看吧,有问题再提吧!