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();
}
}
java -cp ".;asterisk-java.jar" HelloEvents
works fine.
And the class path separator is OS dependent. If you are using linux / mac, use : (colon) instead of ; (semicolon)
We can avoid adding class path every time while compiling or executing code. Now go to the location where jar file is located. Then run,
For linux :- export CLASSPATH=$CLASSPATH:asterisk-java-2.0.3.jar:.
For windows:- set CLASSPATH=$CLASSPATH:asterisk-java-2.0.3.jar:.
Now compile code by
javac HelloEvents.java
Execute it by java HelloEvents
Related
I am studying for the Java 1.8 OCP exam and I ran across something in the Oracle study guide p.459 where it says you can load a remote URL as a FileSystem object. I tried this and can't get it to work. What is the simplest hello-world style answer to get this working?
import java.net.*;
import java.nio.file.*;
public class FileTest {
public static void main(String[] args) throws URISyntaxException, MalformedURLException {
FileSystem remoteFS = FileSystems.getFileSystem(
new URI("http://www.gutenberg.org/files/55007")
);
Path remotePath = remoteFS.getPath("55007-0.txt");
System.out.println(remotePath.isAbsolute());
}
}
It throws the error:
Exception in thread "main" java.nio.file.ProviderNotFoundException:
Provider "http" not found
at java.nio.file.FileSystems.getFileSystem(FileSystems.java:224)
at qa.test.FileTest.main(FileTest.java:7)
Addendum:
FileSystemProvider.installedProviders() returns
[sun.nio.fs.MacOSXFileSystemProvider#4554617c, com.sun.nio.zipfs.ZipFileSystemProvider#74a14482]
I am trying to get a CSV from the report using this well-known plugin and the example from its documentation, but it throws an exception. I wonder why, because I just copied most of the code from the doc.
My code is:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.emitter.csv.CSVRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
public class RunReport {
static void runReport() throws FileNotFoundException, BirtException {
String resourcePath = "C:\\Users\\hpsa\\workspace\\My Reports\\";
FileInputStream fs = new FileInputStream(resourcePath + "new_report_1.rptdesign");
IReportEngine engine = null;
EngineConfig config = new EngineConfig();
config.setEngineHome("C:\\birtre\\birt-runtime-4_3_2\\");
config.setLogConfig("C:\\birtre\\", Level.FINE);
config.setResourcePath(resourcePath);
Platform.startup(config);
IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);
engine.changeLogLevel(Level.FINE);
IReportRunnable design = engine.openReportDesign(fs);
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
CSVRenderOption csvOption = new CSVRenderOption();
String format = CSVRenderOption.OUTPUT_FORMAT_CSV;
csvOption.setOutputFormat(format);
csvOption.setOutputFileName("newBIRTcsv.csv");
csvOption.setShowDatatypeInSecondRow(true);
csvOption.setExportTableByName("SecondTable");
csvOption.setDelimiter("\t");
csvOption.setReplaceDelimiterInsideTextWith("-");
task.setRenderOption(csvOption);
task.setEmitterID("org.eclipse.birt.report.engine.emitter.csv");
task.run();
task.close();
Platform.shutdown();
System.out.println("Report Generated Sucessfully!!");
}
public static void main(String[] args) {
try {
runReport();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am getting the exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/core/runtime/CoreException
at org.eclipse.birt.core.framework.Platform.createPlatformLauncher(Platform.java:115)
at org.eclipse.birt.core.framework.Platform.startup(Platform.java:74)
at com.demshin.birttest.RunReport.runReport(RunReport.java:26)
at com.demshin.birttest.RunReport.main(RunReport.java:55)
Caused by: java.lang.ClassNotFoundException: org.eclipse.core.runtime.CoreException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 4 more
I found the package org.eclipse.core.runtime and registered in the build path, but then I am getting the same exception. Indeed, there are no any CoreException.class in the org.eclipse.core.runtime package. What am I doing wrong?
Setting the engine home is deprecated and most of the time it will prevent the Platform to start, such in your case. Remove this line:
config.setEngineHome("C:\\birtre\\birt-runtime-4_3_2\\");
You just have to ensure the birt runtime 4.3.2 is in the classpath of your context. Furthermore i would recommend you try to generate a native format such pdf first, and then try with a csv format.
I was trying a lot of variants of importing different jars, but finally i found the weird decision here:
go to the Project Explorer, right-mouse click on properties for the
project, go to Java Build path, Click on the External Jars button,
then select all the libraries under
C:\birt_runtime\birt-runtime-2_1_0\ReportEngine\lib\ folder
This made chaos there, but at least it starts.
I'm running hadoop in a school cluster. I get exception in thread main with class not found exception.
Exception in thread "main" java.lang.ClassNotFoundException: movielens.MovieLensDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.apache.hadoop.util.RunJar.main(RunJar.java:153)
But I'm aware that I have to use full package name in the command and i have done the same. Following is the command I used
hadoop jar movielens.jar movielens.MovieLensDriver input output
Following is the code for my driver class.
package movielens;
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.KeyValueTextInputFormat;
import org.apache.hadoop.mapred.jobcontrol.Job;
import org.apache.hadoop.mapred.jobcontrol.JobControl;
public class MovieLensDriver {
public static class JobRunner implements Runnable {
private JobControl control;
public JobRunner(JobControl _control) {
this.control = _control;
}
public void run() {
this.control.run();
}
}
public static void handleRun(JobControl control)
throws InterruptedException {
JobRunner runner = new JobRunner(control);
Thread t = new Thread(runner);
t.start();
while (!control.allFinished()) {
System.out.println("Still running...");
Thread.sleep(5000);
}
}
public static void main(String args[]) throws IOException,
InterruptedException {
System.out.println("Program started");
if (args.length != 2) {
System.err
.println("Usage: MovieLensDriver <input path> <output path>");
System.exit(-1);
}
JobConf conf1 = new JobConf(movielens.MovieLensDriver.class);
conf1.setMapperClass(MoviePairsMapper.class);
conf1.setReducerClass(MoviePairsReducer.class);
conf1.setJarByClass(MovieLensDriver.class);
FileInputFormat.addInputPath(conf1, new Path(args[0]));
FileOutputFormat.setOutputPath(conf1, new Path("temp"));
conf1.setMapOutputKeyClass(Text.class);
conf1.setMapOutputValueClass(Text.class);
conf1.setOutputKeyClass(Text.class);
conf1.setOutputValueClass(IntWritable.class);
JobConf conf2 = new JobConf(MovieLensDriver.class);
conf2.setMapperClass(MoviePairsCoOccurMapper.class);
conf2.setReducerClass(MoviePairsCoOccurReducer.class);
conf2.setJarByClass(MovieLensDriver.class);
FileInputFormat.addInputPath(conf2, new Path("temp"));
FileOutputFormat.setOutputPath(conf2, new Path(args[1]));
conf2.setInputFormat(KeyValueTextInputFormat.class);
conf2.setMapOutputKeyClass(Text.class);
conf2.setMapOutputValueClass(IntWritable.class);
conf2.setOutputKeyClass(Text.class);
conf2.setOutputValueClass(IntWritable.class);
Job job1 = new Job(conf1);
Job job2 = new Job(conf2);
JobControl jobControl = new JobControl("jobControl");
jobControl.addJob(job1);
jobControl.addJob(job2);
job2.addDependingJob(job1);
handleRun(jobControl);
System.out.println("Program complete.");
System.exit(0);
}
}
It has been a frustrating search for the bug for last 3 hours and any help is appreciated.
You can try the 'libjar' option, which will take the jar and place it in the distributed cache. This makes the jar available to all of the job's task attempts. Notice that the libjars argument takes a comma separated list, not a colon or semi-colon separated list.
export LIBJARS=/path/jars1,/path/jars2,/path/movielens.jar
hadoop jar movielens.jar movielens.MovieLensDriver -libjars ${LIBJARS} input output
I've spent almost five hours trying to resolve this, to no avail. I've created an application that uses RMI. It compiles fine, but I can't it get it to run.
All my class files are in C:\Users\Benji\Desktop\ass2\build (short for "assignment"; nothing dirty). All source files are in C:\Users\Benji\Desktop\ass2\src.
I've put everything in a single package to make things more comprehensible (and changed the import statements in the source to reflect this).
I have put a batch file in C:\Users\Benji\Desktop\ass2\ . Its contains the execution statement:
java -classpath ./build -Djava.rmi.server.codebase=file:/C:/Users/Benji/Desktop/ass2/build -Djava.security.policy=broker.policy BrokerReception Broker 16890
(The two args "Broker" and "16890" are needed by the program).
The file broker.policy is also in C:\Users\Benji\Desktop\ass2\ . Its contents are:
grant
{
permission java.security.AllPermission;
};
(and yes, I realise this isn't a good security policy. I'll work on this later).
There are actually three Main classes, one for a client, one for a broker (a mediator for the client) and a server. I am trying to start the Broker. Code for the Broker interface is as follows:
import java.io.FileNotFoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
public interface Broker extends Remote
{
public boolean getAvailability(int startDate, int endDate) throws FileNotFoundException, RemoteException;
public ArrayList<CityInfo> getCities() throws FileNotFoundException, RemoteException;
public ArrayList<HotelInfo> getCityHotels(int cityNumber) throws FileNotFoundException, RemoteException;
public int getHotelRoomRate(int hotelNumber) throws FileNotFoundException, RemoteException;
public boolean makeBooking(String firstName, String lastName, String contact, String creditCardNo) throws FileNotFoundException, RemoteException;
}
And the implementation class:
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class BrokerClientLiaison implements Broker
{
private BrokerDatabase directory;
private BrokerHotelsLiaison liaison;
public BrokerClientLiaison(BrokerDatabase directory, int activeHotelNumber)
{
this.liaison = new BrokerHotelsLiaison(activeHotelNumber);
this.directory = directory;
}
public boolean getAvailability(int startDate, int endDate) throws FileNotFoundException
{
return liaison.getAvailability(startDate, endDate);
}
public ArrayList<CityInfo> getCities() throws FileNotFoundException
{
return directory.getCities();
}
public ArrayList<HotelInfo> getCityHotels(int cityNumber) throws FileNotFoundException
{
return directory.getCityHotels(cityNumber);
}
public int getHotelRoomRate(int hotelNumber) throws FileNotFoundException
{
return liaison.getHotelRoomRate(hotelNumber);
}
public boolean makeBooking(String firstName, String lastName, String contact, String creditCardNo) throws FileNotFoundException
{
return liaison.makeBooking(firstName, lastName, contact, creditCardNo);
}
}
And finally, the main class to start the implementation class:
import java.io.FileNotFoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
public class BrokerReception
{
public static void main(String[] args)
{
System.out.println("Args are:");
for(String arg : args)
{
System.out.println(arg);
}
System.out.println();
try
{
BrokerDatabase directory = new BrokerDatabase();
directory.connect(args[0]);
int activeHotelNumber = Integer.parseInt(args[1]);
if(directory.checkActiveHotelExists(activeHotelNumber))
{
BrokerClientLiaison liaison = new BrokerClientLiaison(directory, activeHotelNumber);
Broker liaisonStub = (Broker) UnicastRemoteObject.exportObject(liaison, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind(Protocol.BROKER_INTERFACE_NAME, liaisonStub);
}
else
{
throw new FileNotFoundException();
}
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
System.err.println("Args required:");
System.err.println("1. Name of database file");
System.err.println("2. Number of active hotel");
System.exit(1);
}
catch(ClassNotFoundException cnfe)
{
System.err.println("Couldn't load database driver");
System.exit(2);
}
catch(SQLException sqle)
{
System.err.println("Couldn't establish connection to database");
System.err.println("Check that the database has been properly registerd,");
System.err.println("and that you provided the correct name");
System.exit(3);
}
catch(NumberFormatException nfe)
{
System.err.println("Second argument must be an integer");
System.exit(4);
}
catch(FileNotFoundException fnfe)
{
System.err.println("The database contains no entries with that hotel number");
System.exit(5);
}
catch(RemoteException re)
{
System.err.println("Unable to bind as " + Protocol.BROKER_INTERFACE_NAME);
re.printStackTrace();
System.exit(6);
}
}
}
"Directory" in the above code is a class that accesses a database.
I don't know what other information I need to give. Can anybody tell me what I'm doing wrong?
Incidentally, I went back and did Oracle's RMI tutorial at http://download.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html, to see if I could figure out what's wrong. The tutorial makes no mention of codebase or security policy, but provides all the code and precise instructions for compilation and execution. I followed these instructions to the letter, and yet even this didn't work!
Stack trace:
java.rmi.ServerException: RemoteException occurred in server thread; nested exce
ption is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
java.lang.ClassNotFoundException: Broker
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:396
)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5
35)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor
t.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec
utor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:908)
at java.lang.Thread.run(Thread.java:662)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
RemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at BrokerReception.main(BrokerReception.java:32)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
ception is:
java.lang.ClassNotFoundException: Broker
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:386
)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5
35)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor
t.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec
utor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: Broker
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:7
11)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:655)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:592)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:6
28)
at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294
)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStrea
m.java:238)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1530)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1492)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
731)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
... 12 more
if you are running your registry as a second process, it needs access to your remote classes. the easiest way to do this is to add the appropriate classpath argument to the commandline when starting the registry.
if you are trying to use remote class loading, i believe you need to setup an rmi security manager in your application, either on the commandline or in the main method. (personally, distributing the classes usually works for 99% of situations and is 100 times easier to get right).
Source fie ->
location c:/program files/java/jdk1.6.0_07/bin
import java.io.File;
import java.io.IOException;
import jxl.*;
public class Geeta {
private String inputFile;
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
public void read() throws Exception {
File inputWorkbook = new File(inputFile);
Workbook w;
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
Cell a1 = sheet.getCell(0,0);
String stringa1 = a1.getContents();
System.out.println(stringa1);
}
public static void main(String[] args) throws Exception {
Geeta test = new Geeta();
test.setInputFile("c:/Progra~1/Java/jdk1.6.0_07/bin/jxlrwtest.xls");
test.read();
}
}
compiled as
javac -classpath jxl.jar Geeta.java
When I try to execute it says.. NoClassDefFoundError
java -jar jxl.jar Geeta
C:\Program Files\Java\jdk1.6.0_07\bin>java -jar jxl.jar Geeta
Exception in thread "main" java.lang.NoClassDefFoundError: Geeta
Caused by: java.lang.ClassNotFoundException: Geeta
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Plz. advice...
I had an error with jxl execution, it was about the CLASSPATH
This works to me:
Change or set the CLASSPATH in Environment Variables:
Open Explorer (Windows-E)
Right-click on Computer
Select Properties
On the "Advanced" Tab select "Environment Variables..."
On System variables window, Edit or Add a new variable named: CLASSPATH
On Variable Value set the place where jxl.jar is, example: .;C:\YourPath\jxl.jar
Open a new CMD window and compile as usual.
Your run command should look like this:
java -classpath jxl.jar;. Geeta