[英]calling args from out side the main method
我正在開發一個客戶端服務器應用程序,可以在其中從遠程客戶端調用服務器,並且從客戶端返回一些字符串。 我正在使用CORBA
。 我有一個通過在Netbeans
上使用Java Swing
開發的用戶界面。 當單擊客戶端界面上的按鈕時,我需要調用服務器。 為此,我必須將以下代碼段放入jButton
動作偵聽器中。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
// Use NamingContextExt instead of NamingContext. This is
// part of the Interoperable naming Service.
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
// resolve the Object Reference in Naming
String name = "Hello";
bsImpl = BubbleSortHelper.narrow(ncRef.resolve_str(name));
//System.out.println("Obtained a handle on server object: " + helloImpl);
String z = bsImpl.sort(inputFlArray);
System.out.println(z);
bsImpl.shutdown();
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
編譯后,出現錯誤,提示無法識別args。 我只是復制ORB orb = ORB.init(args, null);
代碼段位於main方法內部的某個位置。 我知道出現錯誤是因為我在main方法之外使用了args。 我需要知道如何在main方法之外在這里初始化ORB
對象?
String[] args
傳遞給您的main()
方法。 我建議您在那里初始化ORB,並使用類似以下內容的方法將實例傳遞給JButton
的構造函數:
public static void main(String[] args) {
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// ....
JButton myButton = new MyButton(orb);
// ....
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
實際上,如果要在每個客戶端操作中使用ORB的新實例。 您可以按照以下步驟進行操作:
ORB orb = org.omg.CORBA.ORB.init(new String[0], null);
公共靜態ORB init(String [] args,屬性道具)
為獨立應用程序創建新的ORB實例。 只能從應用程序中調用此方法,並且每次調用該方法時都會返回一個新的功能齊全的ORB對象。
參數:
args-應用程序的main方法的命令行參數; 可能為空
道具 -特定於應用的屬性; 可能為空
在這里,args可以為null,但是,如果直接使用null,它將與另一個方法org.omg.CORBA.ORB.init(Applet app, Properties props)
產生歧義。
因此,您可以改用emyty String數組。
通常,不使用args,如果要使用它,可以將其傳遞給“ jButtonaction listener”類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.