Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 when I run my web service client

StackOverflow https://stackoverflow.com/questions/15878812

  •  02-04-2022
  •  | 
  •  

Вопрос

I create a Dynamic web project with name 'testUpdate' (and of course I don't forget to change the dynamic web module version to 2.5 and in configuration I choose Axis 2 web service

  1. I add to my Dynamic web project these two classes :

SimpleService .java and PWCBHandler.java

  1. I right click on SimpleService.java -> New ->Other -> Web Service to create my web service

  2. I don't forget to copy all the jar files from rampart distribution to testUpdate/ WebContent/WEB_INF/lib and all .mar modules into testUpdate/ WebContent/WEB_INF/modules

  3. I change services.xml file so it looks like

    <service name="SimpleService" >
    <module ref="rampart" />
    <Description>
    
    </Description>
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
        <messageReceiver  mep="http://www.w3.org/2004/08/wsdl/in-out"  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>
    <parameter name="ServiceClass" locked="false">com.gismo.SimpleService</parameter>
        <parameter name="InflowSecurity">
        <action>
            <items>UsernameToken</items>
            <passwordCallbackClass>com.gismo.PWCBHandler</passwordCallbackClass>
        </action>
    </parameter>
    </service>
    
  4. I right click on testUpdate -> RUN AS _> Run on Server (and my web service is deployed successfully)

  5. File -> New -> Other -> Web Service Client

and in Service Definition I paste the url of the wsdl file of SimpleService

( http://localhost:9091/testUpdate/services/SimpleService?wsdl)

  1. I add testcl.java class to my web-service client. Here is the code

    public class testCL {
    
    public static void main(String[] args) throws Exception {
    
    if (args.length != 2) {
    
        System.out.println(args.length);
        System.out
                .println("Usage: $java Client endpoint_address client_repo_path");
    }
    
    ConfigurationContext ctx = ConfigurationContextFactory
            .createConfigurationContextFromFileSystem(args[1], args[1]
                    + "/conf/axis2.xml");
    
    ServiceClient client = new ServiceClient(ctx, null);
    Options options = new Options();
    options.setAction("urn:echo");
    options.setTo(new EndpointReference(args[0]));
    client.setOptions(options);
    
    OMElement response = client.sendReceive(getPayload("Hello world"));
    
    System.out.println(response);
    }
    
      private static OMElement getPayload(String value) {
     OMFactory factory = OMAbstractFactory.getOMFactory();
     OMNamespace ns = factory.createOMNamespace("com.gismo/xsd", "ns1");
     OMElement elem = factory.createOMElement("echo", ns);
     OMElement childElem = factory.createOMElement("param0", null);
     childElem.setText(value);
     elem.addChild(childElem);
     return elem;
    }
    }
    
  2. I don't forget to change webSercice_client/WebContent/axis2-web/conf/axis2.xml and add

     <module ref="rampart"/>
     <parameter name="OutflowSecurity">
      <action>
        <items>UsernameToken</items>
        <user>bob</user>
         <passwordCallbackClass>com.gismo.PWCBHandler</passwordCallbackClass>
      </action>
     </parameter>
    
  3. But when I run testCl as Java Application it gives me an exception

    Usage: $java Client endpoint_address client_repo_path Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at com.gismo.testcl.main(testcl.java:24)

Это было полезно?

Решение

My psychic debugging powers tell me that you ran it without providing two command-line arguments. You can see the error message "Usage: $java Client endpoint_address client_repo_path" is present in your program output, meaning that you didn't supply two command-line arguments, so args[1] may not be valid. Your program doesn't exit after checking the number of command-line arguments, so it tries to access args[1] after complaining that the program was run incorrectly.

if (args.length != 2) {

    System.out.println(args.length);
    System.out
            .println("Usage: $java Client endpoint_address client_repo_path");
}

ConfigurationContext ctx = ConfigurationContextFactory
        .createConfigurationContextFromFileSystem(args[1], args[1]
                + "/conf/axis2.xml");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top