简体   繁体   中英

Pass argument in main method when class extends another class in Java

I am trying to run a script for asterisk-java as below. I have added a main method and calling the service method inside it as follows:

import org.asteriskjava.fastagi.AgiChannel;
import org.asteriskjava.fastagi.AgiException;
import org.asteriskjava.fastagi.AgiRequest;
import org.asteriskjava.fastagi.BaseAgiScript;

public class HelloAgiScript extends BaseAgiScript
{
    public void service(AgiRequest request, AgiChannel channel)
            throws AgiException
    {
        // Answer the channel...
        answer();

        // ...say hello...
        streamFile("welcome");

    // ...and hangup.
        hangup();
    }

    public static void main (String[] args) 
    {
    HelloAgiScript asteriskService = new HelloAgiScript();
    asteriskService.service(request, channel);
    }    
}

When I try to compile it with the following command:

javac -cp asterisk-java.jar HelloAgiScript.java

I get this error:

HelloAgiScript.java:24: error: cannot find symbol
        asteriskService.service(request, channel);
                                ^
  symbol:   variable request
  location: class HelloAgiScript
HelloAgiScript.java:24: error: channel has private access in AgiOperations
        asteriskService.service(request, channel);
                                         ^
2 errors

How can I pass the arguments to the instance of the service method inside the main method?

You need to pass the argument as the object of AgiRequest and AgiChannel class to service() method call.

As in your case both request and channel variable is not created. That's why you are getting error Can't find symbol

Your main method should be like this:

public static void main (String[] args) 
{
    HelloAgiScript asteriskService = new HelloAgiScript();
    AgiRequest request = new AgiRequest();
    AgiChannel channel = new AgiChannel();
    asteriskService.service(request, channel);
 }  

You didn't put any parameters into your main method from the command line. You should write something like

javac -cp asterisk-java.jar par1, par2

But, first of all, you should define which parameter should be your inner parameter like

public static void main (String[] args) 
{
    HelloAgiScript asteriskService = new HelloAgiScript();
    AgiRequest request = args[0];
    AgiChannel channel = args[1];
    asteriskService.service(request, channel);
 } 

Look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM