问题
I am developing a client server application where i can invoke server from the remote client and the some string is returned from the client. I am using CORBA
. I have a user interface developed by using Java Swing
on Netbeans
. I need to invoke the server when a button is clicked on the client interface. For that I have to put following code segment inside jButton
action listener.
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);
}
}
Once I compile it, I get the error saying args cannot be identified. I just copied ORB orb = ORB.init(args, null);
code segment from a place where it was inside the main method. I know that error comes up because of I used args outside the main method.
I need to know that how can I initialize ORB
object here outside the main method?
回答1:
String[] args
are passed to your main()
method. I would recommend you initialize your ORB there, and pass the instance to your JButton
's constructor with something like -
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);
}
}
回答2:
Actually, if you want a new instance of ORB in every client's action. You could do it as below:
ORB orb = org.omg.CORBA.ORB.init(new String[0], null);
public static ORB init(String[] args, Properties props)
Creates a new ORB instance for a standalone application. This method may be called from applications only and returns a new fully functional ORB object each time it is called.
Parameters:
args - command-line arguments for the application's main method; may be null
props - application-specific properties; may be null
Here, args could be null, however, if you use null dirctly, it will be ambiguous with another method org.omg.CORBA.ORB.init(Applet app, Properties props)
Therefore, you could use emyty String arrays instead.
Normally, the args is not used, if you want to use it, you could pass it to your "jButtonaction listener" class.
来源:https://stackoverflow.com/questions/25245355/calling-args-from-out-side-the-main-method