[英]Unable to close the command prompt after executing a batch file using java
[英]On executing batch file capture command prompt logs and save a backup of those logs in file using Java
要打印日志文件,我使用了以下代碼,但無法打印批處理文件代碼中存在的“ System.out.println”語句。
Runtime runtime = Runtime.getRuntime();
Process p1 = runtime.exec("C:\\Users\\mf11131\\Desktop\\Batch\\SimpleBatch.bat");
java.io.InputStream is = (java.io.InputStream) p1.getInputStream();
File logfile = new File("C:\\logs\\Logfile.txt");
FileOutputStream fop = new FileOutputStream(logfile);
int i = 0;
while((i = is.read()) != -1)
{
fop.write((char)i);
}
下面是批處理文件的簡單代碼。
class abc
{
public static void main(String args[])
{
int count=0;
for(int i=0;i<10;i++)
{
System.out.println("hi hello"+count);
}
}
}
如果要將所有輸出都放入文件中,則應使用像這樣的流重定向
byBatFile.bat > logFile.
這將導致所有輸出都存儲到logFile
。
我懷疑是否有可能在批處理腳本中獲取被調用應用程序的stdio流。 我認為IO是按照您希望使用的方式分離的。
另一方面,您的批處理腳本會在每個命令上“產生”新進程。 您可以嘗試call
這些過程為生成新的過程,例如這樣
call javac abc.java
call java abc
我找到了解決方案,因為在創建批處理文件時將目錄指向Java類目錄時犯了一個錯誤。 因此,僅Java(System.out.println)語句不打印。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.