本文目錄一覽:
- 1、Java里如何獲取調用此方法的方法?
- 2、如何在Java中執行其它程序
- 3、java 如何只知類名和方法名得到對象和執行方法
- 4、關於用java反射調用一個類裏面的方法並執行
- 5、Java eclipse如何獲取執行結果
- 6、Java回調以及如何獲取線程的執行結果
Java里如何獲取調用此方法的方法?
心情好 告訴你吧 Thread.currentThread().getStackTrace()[1]是你當前方法執行堆棧 Thread.currentThread().getStackTrace()[2]就是上一級的方法堆棧 以此類推
如何在Java中執行其它程序
在編寫Java程序時,有時候需要在Java程序中執行另外一個程序。
1、啟動程序Java提供了兩種方法用來啟動其它程序:
(1)使用Runtime的exec()方法
(2)使用ProcessBuilder的start()方法
不管在哪種操作系統下,程序具有基本類似的一些屬性。一個程序啟動後就程序操作系統的一個進程,進程在執行的時候有自己的環境變量、有自己的工作目錄。Runtime和ProcessBuilder提供了不同的方式來啟動程序,設置啟動參數、環境變量和工作目錄。
能夠在Java中執行的外部程序,必須是一個實際存在的可執行文件,對於shell下的內嵌命令是不能直接執行的。
採用Runtime的exec執行程序時,首先要使用Runtime的靜態方法得到一個Runtime,然後調用Runtime的exec方
法。可以將要執行的外部程序和啟動參數、環境變量、工作目錄作為參數傳遞給exec方法,該方法執行後返回一個Process代表所執行的程序。
Runtime有六個exec方法,其中兩個的定義為:
public Process exec(String[] cmdarray, String[] envp, File dir)
public Process exec(String command, String[] envp, File dir)
cmdarray和command為要執行的命令,可以將命令和參數作為一個字符串command傳遞給exec()方法,也可以將命令和參數一個一個的方在數組cmdarray里傳遞給exec()方法。
envp為環境變量,以name=value的形式放在數組中。dir為工作目錄。
可以不要dir參數,或者不要envp和dir參數,這樣就多出了其它4個exec()方法。如果沒有dir參數或者為null,那麼新啟動的
進程就繼承當前java進程的工作目錄。如果沒有envp參數或者為null,那麼新啟動的進程就繼承當前java進程的環境變量。
也可以使用ProcessBuilder類啟動一個新的程序,該類是後來添加到JDK中的,而且被推薦使用。通過構造函數設置要執行的命令以及
參數,或者也可以通過command()方法獲取命令信息後在進行設置。通過directory(File directory)
方法設置工作目錄,通過environment()獲取環境變量信息來修改環境變量。
在使用ProcessBuilder構造函數創建一個新實例,設置環境變量、工作目錄後,可以通過start()方法來啟動新程序,與Runtime的exec()方法一樣,該方法返回一個Process對象代表啟動的程序。
ProcessBuilder與Runtime.exec()方法的不同在於ProcessBuilder提供了
redirectErrorStream(boolean redirectErrorStream)
方法,該方法用來將進程的錯誤輸出重定向到標準輸出里。即可以將錯誤輸出都將與標準輸出合併。
2、Process
不管通過那種方法啟動進程後,都會返回一個Process類的實例代表啟動的進程,該實例可用來控制進程並獲得相關信息。Process 類提供了執行從進程輸入、執行輸出到進程、等待進程完成、檢查進程的退出狀態以及銷毀(殺掉)進程的方法:
(1) void destroy()
殺掉子進程。
一般情況下,該方法並不能殺掉已經啟動的進程,不用為好。
(2) int exitValue()
返回子進程的出口值。
只有啟動的進程執行完成、或者由於異常退出後,exitValue()方法才會有正常的返回值,否則拋出異常。
(3)InputStream getErrorStream()
獲取子進程的錯誤流。
如果錯誤輸出被重定向,則不能從該流中讀取錯誤輸出。
(4)InputStream getInputStream()
獲取子進程的輸入流。
可以從該流中讀取進程的標準輸出。
(5)OutputStream getOutputStream()
獲取子進程的輸出流。
寫入到該流中的數據作為進程的標準輸入。
(6) int waitFor()
導致當前線程等待,如有必要,一直要等到由該 Process 對象表示的進程已經終止。
通過該類提供的方法,可以實現與啟動的進程之間通信,達到交互的目的。
3、從標準輸出和錯誤輸出流讀取信息
從啟動其他程序的Java進程看,已啟動的其他程序輸出就是一個普通的輸入流,可以通過getInputStream()和getErrorStream來獲取。
對於一般輸出文本的進程來說,可以將InputStream封裝成BufferedReader,然後就可以一行一行的對進程的標準輸出進行處理。
4、舉例
(1)Runtime.exec()
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
public class Test1 {
public static void main(String[] args) {
try {
Process p = null;
String line = null;
BufferedReader stdout = null;
//list the files and directorys under C:\
p = Runtime.getRuntime().exec(“CMD.exe /C dir”, null, new File(“C:\\”));
stdout = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
stdout.close();
//echo the value of NAME
p = Runtime.getRuntime().exec(“CMD.exe /C echo %NAME%”, new String[] {“NAME=TEST”});
stdout = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
stdout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
(2)ProcessBuilder
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
try {
List list = new ArrayList();
ProcessBuilder pb = null;
Process p = null;
String line = null;
BufferedReader stdout = null;
//list the files and directorys under C:\
list.add(“CMD.EXE”);
list.add(“/C”);
list.add(“dir”);
pb = new ProcessBuilder(list);
pb.directory(new File(“C:\\”));
p = pb.start();
stdout = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
stdout.close();
//echo the value of NAME
pb = new ProcessBuilder();
mand(new String[] {“CMD.exe”, “/C”, “echo %NAME%”});
pb.environment().put(“NAME”, “TEST”);
p = pb.start();
stdout = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
stdout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、獲取進程的返回值
通常,一個程序/進程在執行結束後會向操作系統返回一個整數值,0一般代表執行成功,非0表示執行出現問題。有兩種方式可以用來獲取進程的返回
值。一是利用waitFor(),該方法是阻塞的,執導進程執行完成後再返回。該方法返回一個代表進程返回值的整數值。另一個方法是調用
exitValue()方法,該方法是非阻塞的,調用立即返回。但是如果進程沒有執行完成,則拋出異常。
6、阻塞的問題
由Process代表的進程在某些平台上有時候並不能很好的工作,特別是在對代表進程的標準輸入流、輸出流和錯誤輸出進行操作時,如果使用不慎,有可能導致進程阻塞,甚至死鎖。
如果將以上事例中的從標準輸出重讀取信息的語句修改為從錯誤輸出流中讀取:
stdout = new BufferedReader(new InputStreamReader(p
.getErrorStream()));
那麼程序將發生阻塞,不能執行完成,而是hang在那裡。
當進程啟動後,就會打開標準輸出流和錯誤輸出流準備輸出,當進程結束時,就會關閉他們。在以上例子中,錯誤輸出流沒有數據要輸出,標準輸出流中
有數據輸出。由於標準輸出流中的數據沒有被讀取,進程就不會結束,錯誤輸出流也就不會被關閉,因此在調用readLine()方法時,整個程序就會被阻
塞。為了解決這個問題,可以根據輸出的實際先後,先讀取標準輸出流,然後讀取錯誤輸出流。
但是,很多時候不能很明確的知道輸出的先後,特別是要操作標準輸入的時候,情況就會更為複雜。這時候可以採用線程來對標準輸出、錯誤輸出和標準輸入進行分別處理,根據他們之間在業務邏輯上的關係決定讀取那個流或者寫入數據。
針對標準輸出流和錯誤輸出流所造成的問題,可以使用ProcessBuilder的redirectErrorStream()方法將他們合二為一,這時候只要讀取標準輸出的數據就可以了。
當在程序中使用Process的waitFor()方法時,特別是在讀取之前調用waitFor()方法時,也有可能造成阻塞。可以用線程的方法來解決這個問題,也可以在讀取數據後,調用waitFor()方法等待程序結束。
總之,解決阻塞的方法應該有兩種:
(1)使用ProcessBuilder類,利用redirectErrorStream方法將標準輸出流和錯誤輸出流合二為一,在用start()方法啟動進程後,先從標準輸出中讀取數據,然後調用waitFor()方法等待進程結束。
如:
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test3 {
public static void main(String[] args) {
try {
List list = new ArrayList();
ProcessBuilder pb = null;
Process p = null;
String line = null;
BufferedReader stdout = null;
//list the files and directorys under C:\
list.add(“CMD.EXE”);
list.add(“/C”);
list.add(“dir”);
pb = new ProcessBuilder(list);
pb.directory(new File(“C:\\”));
//merge the error output with the standard output
pb.redirectErrorStream(true);
p = pb.start();
//read the standard output
stdout = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
int ret = p.waitFor();
System.out.println(“the return code is ” + ret);
stdout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
(2)使用線程
import java.util.*;
import java.io.*;
class StreamWatch extends Thread {
InputStream is;
String type;
List output = new ArrayList();
boolean debug = false;
StreamWatch(InputStream is, String type) {
this(is, type, false);
}
StreamWatch(InputStream is, String type, boolean debug) {
this.is = is;
this.type = type;
this.debug = debug;
}
public void run() {
try {
PrintWriter pw = null;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
output.add(line);
if (debug)
System.out.println(type + “” + line);
}
if (pw != null)
pw.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public List getOutput() {
return output;
}
}
public class Test5 {
public static void main(String args[]) {
try {
List list = new ArrayList();
ProcessBuilder pb = null;
Process p = null;
// list the files and directorys under C:\
list.add(“CMD.EXE”);
list.add(“/C”);
list.add(“dir”);
pb = new ProcessBuilder(list);
pb.directory(new File(“C:\\”));
p = pb.start();
// process error and output message
StreamWatch errorWatch = new StreamWatch(p.getErrorStream(),
“ERROR”);
StreamWatch outputWatch = new StreamWatch(p.getInputStream(),
“OUTPUT”);
// start to watch
errorWatch.start();
outputWatch.start();
//wait for exit
int exitVal = p.waitFor();
//print the content from ERROR and OUTPUT
System.out.println(“ERROR: ” + errorWatch.getOutput());
System.out.println(“OUTPUT: ” + outputWatch.getOutput());
System.out.println(“the return code is ” + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
7、在Java中執行Java程序
執行一個Java程序的關鍵在於:
(1)知道JAVA虛擬機的位置,即java.exe或者java的路徑
(2)知道要執行的java程序的位置
(3)知道該程序所依賴的其他類的位置
舉一個例子,一目了然。
(1)待執行的Java類
public class MyTest {
public static void main(String[] args) {
System.out.println(“OUTPUT one”);
System.out.println(“OUTPUT two”);
System.err.println(“ERROR 1”);
System.err.println(“ERROR 2”);
for(int i = 0; i args.length; i++)
{
System.out.printf(“args[%d] = %s.”, i, args[i]);
}
}
}
(2)執行該類的程序
import java.util.*;
import java.io.*;
class StreamWatch extends Thread {
InputStream is;
String type;
List output = new ArrayList();
boolean debug = false;
StreamWatch(InputStream is, String type) {
this(is, type, false);
}
StreamWatch(InputStream is, String type, boolean debug) {
this.is = is;
this.type = type;
this.debug = debug;
}
public void run() {
try {
PrintWriter pw = null;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
output.add(line);
if (debug)
System.out.println(type + “” + line);
}
if (pw != null)
pw.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public List getOutput() {
return output;
}
}
public class Test6 {
public static void main(String args[]) {
try {
List list = new ArrayList();
ProcessBuilder pb = null;
Process p = null;
String java = System.getProperty(“java.home”) + File.separator + “bin” + File.separator + “java”;
String classpath = System.getProperty(“java.class.path”);
// list the files and directorys under C:\
list.add(java);
list.add(“-classpath”);
list.add(classpath);
list.add(MyTest.class.getName());
list.add(“hello”);
list.add(“world”);
list.add(“good better best”);
pb = new ProcessBuilder(list);
p = pb.start();
System.out.println(mand());
// process error and output message
StreamWatch errorWatch = new StreamWatch(p.getErrorStream(),
“ERROR”);
StreamWatch outputWatch = new StreamWatch(p.getInputStream(),
“OUTPUT”);
// start to watch
errorWatch.start();
outputWatch.start();
//wait for exit
int exitVal = p.waitFor();
//print the content from ERROR and OUTPUT
System.out.println(“ERROR: ” + errorWatch.getOutput());
System.out.println(“OUTPUT: ” + outputWatch.getOutput());
System.out.println(“the return code is ” + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
java 如何只知類名和方法名得到對象和執行方法
Class t = Class.forName(”包名.TEST”); Method m = t.getMethod(”getDetail”,null); m.invoke(); 方法有參數沒啊?有的話把null換成參數列表,注意是參數對應的Class類組成的對象數組!
關於用java反射調用一個類裏面的方法並執行
Java中要用到反射,首先就必須要獲取到對應的class對象,在Java中有三種方法獲取類對應的class對象。
1、通過類的.class屬性
2、通過類實例的getClass()方法獲取
3、通過Class.forName(String className)方法獲取
現在比如在package下有個類Calculator
public class Calculator{
public double add(double score1,double score2){
return score1 + score2;
}
public void print(){
System.out.println(“OK”);
}
public static double mul(double score1,double score2){
return score1 * score2;
}
}
public class CalculatorTest {
public static void main(String[] args) throws Exception {
//通過類的.class屬性獲取
ClassCalculator clz = Calculator.class;
//或者通過類的完整路徑獲取,這個方法由於不能確定傳入的路徑是否正確,這個方法會拋ClassNotFoundException
// ClassCalculator clz = Class.forName(“test.Calculator”);
//或者new一個實例,然後通過實例的getClass()方法獲取
// Calculator s = new Calculator();
// ClassCalculator clz = s.getClass();
//1. 獲取類中帶有方法簽名的mul方法,getMethod第一個參數為方法名,第二個參數為mul的參數類型數組
Method method = clz.getMethod(“mul”, new Class[]{double.class,double.class});
//invoke 方法的第一個參數是被調用的對象,這裡是靜態方法故為null,第二個參數為給將被調用的方法傳入的參數
Object result = method.invoke(null, new Object[]{2.0,2.5});
//如果方法mul是私有的private方法,按照上面的方法去調用則會產生異常NoSuchMethodException,這時必須改變其訪問屬性
//method.setAccessible(true);//私有的方法通過發射可以修改其訪問權限
System.out.println(result);//結果為5.0
//2. 獲取類中的非靜態方法
Method method_2 = clz.getMethod(“add”, new Class[]{double.class,double.class});
//這是實例方法必須在一個對象上執行
Object result_2 = method_2.invoke(new Calculator(), new Object[]{2.0,2.5});
System.out.println(result_2);//4.5
//3. 獲取沒有方法簽名的方法print
Method method_3 = clz.getMethod(“print”, new Class[]{});
Object result_3 = method_3.invoke(new Calculator(), null);//result_3為null,該方法不返回結果
}
}
Java eclipse如何獲取執行結果
這個問題有點不太明確呢,我就我的見解說吧,可以利用java的I/O類,其實一般沒有編可視的界面時是在下面那個方框看的,用相關輸出方法在那裡顯示可見,一般是這樣的,但是我個人覺得有個很好的東西可以來顯示你的結果··那就是有Applet顯示,不用太多的代碼,就幾行而已,其實用applet來測試自己的運行結果很好···就這些了,
Java回調以及如何獲取線程的執行結果
軟件模塊之間存在調用的接口,從調用方式來看,有同步調用、回調、異步調用這三種方式:
同步調用是是一種阻塞式調用,調用方要等待被調用方執行完畢返回後才能獲取調用的執行結果,是一種單向調用。
回調是一種雙向調用,調用方在執行被調用方後,被調用方會調用被調用方的接口;
異步調用是一種類似消息或者事件的機制,接口在收到某個消息或發生某事件時,會主動通知客戶方,通常使用回調來實現異步調用。
Java回調的必須要素:
1.僱主類必須有可以被觀察者調用的方法A;
2.觀察者必須持有可以調用A的對象的引用。
在實際工作中,我們通常將方法A以interface或者內部類的形式來實現,然後把包含有A的類的對象引用傳遞到觀察者中。
Java中的線程的返回值是void,並且是一個異步執行流,所以我們沒有直接的方法來獲取線程執行後的結果,即不能直接知道線程何時結束,以及合適去獲取線程執行任務後的結果。由於回調的存在,我們可以在線程中以回調的方式通知線程的調用者線程的結束時間,並可以將任務的結果通過回調回送到調用者中。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/285060.html