我是星号的绝对新手。我正在尝试使用asterisk-java来通过AMI监听事件。我目前正在使用版本11.2.1星号。当我尝试将代码编译为
时javac -cp asterisk-java-0.3.jar HelloEvents.java
它成功完成。但是当我尝试执行该文件时,我发出以下错误。
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.
代码是
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();
}
}
答案 0 :(得分:1)
java -cp ".;asterisk-java.jar" HelloEvents
工作正常。 类路径分隔符取决于操作系统。如果您使用的是linux / mac,请使用:(冒号)代替; (分号)
答案 1 :(得分:0)
我们可以避免在编译或执行代码时每次添加类路径。现在转到jar文件所在的位置。然后运行,
对于linux:-export CLASSPATH=$CLASSPATH:asterisk-java-2.0.3.jar:.
对于Windows:-set CLASSPATH=$CLASSPATH:asterisk-java-2.0.3.jar:.
现在通过
编译代码
javac HelloEvents.java
通过java HelloEvents