gpt4 book ai didi

java - 从 main 方法外部调用 args

转载 作者:行者123 更新时间:2023-12-01 12:39:33 25 4
gpt4 key购买 nike

我正在开发一个客户端服务器应用程序,我可以从远程客户端调用服务器,并从客户端返回一些字符串。我正在使用 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 无法识别。我刚刚从 main 方法内部的位置复制了 ORB orb = ORB.init(args, null); 代码段。我知道出现错误是因为我在 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);
}
}

关于java - 从 main 方法外部调用 args,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com