当类扩展Java中的另一个类时,在main方法中传递参数

时间:2019-04-22 06:51:34

标签: java

我正尝试为asterisk-java运行以下脚本。我添加了一个main方法并在其中调用了service方法,如下所示:

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);
    }    
}

当我尝试使用以下命令进行编译时:

javac -cp asterisk-java.jar HelloAgiScript.java

我收到此错误:

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

如何将参数传递给service方法内的main方法的实例?

2 个答案:

答案 0 :(得分:2)

您需要将参数作为AgiRequestAgiChannel类的对象传递给service()方法调用。

与您的情况一样,requestchannel变量均未创建。这就是为什么出现错误Can't find symbol

的原因

您的主要方法应如下:

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

答案 1 :(得分:-1)

您没有从命令行将任何参数放入您的main方法中。你应该写类似

javac -cp asterisk-java.jar par1, par2

但是,首先,您应该定义哪个参数应该是内部参数,例如

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

here