1

I am an absolute newbee in asterisk. I am trying to use asterisk-java for event listening through AMI. I am currently using the version 11.2.1 asterisk. When I tries to compiles the code as

javac -cp asterisk-java-0.3.jar HelloEvents.java

it complets successfully. But when I try to execute the file, I give the following error.

Exception in thread "main" java.lang.NoClassDefFoundError: HelloEvents
Caused by: java.lang.ClassNotFoundException: HelloEvents
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: HelloEvents. Program will exit.

the code is

import java.io.IOException;

import org.asteriskjava.manager.AuthenticationFailedException;

import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.ManagerEventListener;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.StatusAction;
import org.asteriskjava.manager.event.ManagerEvent;

public class HelloEvents implements ManagerEventListener
{
    private ManagerConnection managerConnection;

    public HelloEvents() throws IOException
    {
        ManagerConnectionFactory factory = new ManagerConnectionFactory(
                "localhost", "manager", "password");

        this.managerConnection = factory.createManagerConnection();
    }

    public void run() throws IOException, AuthenticationFailedException,
            TimeoutException, InterruptedException
    {
        // register for events
        managerConnection.addEventListener(this);

        // connect to Asterisk and log in
        managerConnection.login();

        // request channel state
        managerConnection.sendAction(new StatusAction());

        // wait 10 seconds for events to come in
        Thread.sleep(10000);

        // and finally log off and disconnectaaaa
        managerConnection.logoff();
    }

    public void onManagerEvent(ManagerEvent event)
    {
        // just print received events
        System.out.println(event);
    }

    public static void main(String[] args) throws Exception
    {
        HelloEvents helloEvents;

        helloEvents = new HelloEvents();
        helloEvents.run();
    }
}
4

2 回答 2

2
java -cp ".;asterisk-java.jar" HelloEvents

工作正常。并且类路径分隔符取决于操作系统。如果您使用的是 linux / mac,请使用 :(冒号)而不是 ; (分号)

于 2015-10-21T05:29:19.267 回答
0

我们可以避免每次编译或执行代码时添加类路径。现在转到jar文件所在的位置。然后运行,

对于Linux:-export CLASSPATH=$CLASSPATH:asterisk-java-2.0.3.jar:.

对于窗户:-set CLASSPATH=$CLASSPATH:asterisk-java-2.0.3.jar:.

现在编译代码
javac HelloEvents.java

执行它java HelloEvents

于 2020-02-14T20:21:01.567 回答