使用Process物件反編譯一個.class型別檔案,行程拋出一個例外:
// 運行作業系統命令,并將輸出發送到控制臺。
package io;
import java.io.*;
public class OSExecute {
public static void command(String command) {
boolean err = false;
try {
// 根據傳入的命令序列,創建一個行程
Process process = new ProcessBuilder(command.split(" ")).start();
// 獲得該行程的輸出流
BufferedReader results = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String s;
while((s = results.readLine()) != null)
System.out.println(s);
// 獲得該行程的錯誤流
BufferedReader errors = new BufferedReader(
new InputStreamReader(process.getErrorStream(), "GBK"));
// 如果出現問題,報告錯誤并將非零值回傳給呼叫行程:
while((s = errors.readLine()) != null) {
System.err.println(s);
err = true;
}
} catch(Exception e) {
// 如果是Windows 2000,它會拋出默認命令列的例外:
if(!command.startsWith("CMD /C"))
command("CMD /C" + command);
else
throw new RuntimeException(e);
}
// 如果行程自身在執行程序中產生錯誤,則拋出單獨定制的運行時例外
if(err)
throw new OSExecuteException("Errors executing " + command);
}
}
package io;
public class OSExecuteDemo {
public static void main(String[] args) {
// 反編譯OSExecuteDemo
OSExecute.command("javap /bin/io/OSExecuteDemo");
}
}
運行該程式后,結果如下:
Compiled from "OSExecuteDemo.java"
public class io.OSExecuteDemo {
public io.OSExecuteDemo();
public static void main(java.lang.String[]);
}
警告: 二進制檔案/bin/io/OSExecuteDemo包含io.OSExecuteDemo
Exception in thread "main" io.OSExecuteException: Errors executing javap /bin/io/OSExecuteDemo
at io.OSExecute.command(OSExecute.java:34)
at io.OSExecuteDemo.main(OSExecuteDemo.java:6)
反編譯命令執行正常,但老是拋出一個例外。請問這是什么原因造成的?如何解決?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/235705.html
標籤:Java相關
上一篇:猜數游戲
下一篇:vertx關于異步的初級問題
