So im using Smack to run my chat bot for league of legends, however I can't even get the bot to show up because of a missing class error that I can't seem to figure out. Code and error below, Thanks for any help, -Nick
Also: yes, this code was taken from an example because when I tried it myself I still got the same error.
package com.nickparks.bot;
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
public class JabberSmackAPI implements MessageListener{
XMPPConnection connection;
public void login(String userName, String password) throws XMPPException
{
ConnectionConfiguration config = new ConnectionConfiguration("chat.na1.lol.riotgames.com",5223);
connection = new XMPPTCPConnection(config);
try {
connection.connect();
connection.login(userName, password, "xiff");
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void displayBuddyList()
{
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for(RosterEntry r:entries)
{
System.out.println(r.getUser());
}
}
public void disconnect()
{
try {
connection.disconnect();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}
public void processMessage(Chat chat, Message message)
{
if(message.getType() == Message.Type.chat)
System.out.println(chat.getParticipant() + " says: " + message.getBody());
}
public static void main(String args[]) throws XMPPException, IOException
{
// declare variables
JabberSmackAPI c = new JabberSmackAPI();
// Enter your login information here
c.login("bot", "Password");
c.displayBuddyList();
System.out.println("-----");
System.out.println("Who do you want to talk to? - Type contacts full email address:");
String talkTo = br.readLine();
System.out.println("-----");
System.out.println("All messages will be sent to " + talkTo);
System.out.println("Enter your message in the console:");
System.out.println("-----\n");
while( !(msg=br.readLine()).equals("bye"))
{
System.out.println("test");
}
c.disconnect();
System.exit(0);
}
}
And here's the error I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:321)
atorg.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:316)
at org.jivesoftware.smack.SmackConfiguration.<clinit>(SmackConfiguration.java:148)
at org.jivesoftware.smack.ConnectionConfiguration.<init>(ConnectionConfiguration.java:65)
at com.nickparks.bot.JabberSmackAPI.login(JabberSmackAPI.java:16)
at com.nickparks.bot.JabberSmackAPI.main(JabberSmackAPI.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory
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)
... 11 more
You need to have XPP3 (XML Pull Parser 3) in your classpath. Smack 4 does no longer bundle it (unlike Smack 3).
I also recommend using a build system with dependency resolution like maven or gradle, which would automatically fetch the required dependencies.
Related
I am trying to run the rmi tutorial on oracles' website. I am able to run the server, but I receive an error running the client. The error I receive when try to run the cleint is a noNotBoundException exception. How do I fix this error?
Below is the code and exception
Exception
Client exception: java.rmi.NotBoundException: Hello
java.rmi.NotBoundException: Hello
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:166)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:411)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:272)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at example.hello.Client.main(Client.java:52)
Server
package example.hello;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Client
package example.hello;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Interface
package example.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
You need to launch the [RMI] registry. Method getRegistry() does not launch the registry. Method createRegistry(int) launches the registry. You need to change this line in your class Server
Registry registry = LocateRegistry.getRegistry();
to this
Registry registry = LocateRegistry.createRegistry(1099);
Note that 1099 is the default port used by RMI.
i want to create a program to play shoutcast stream.
i copy this code from here and i heve the below error message.
i use BasicPlayer library, if you have any other library to suggest, it will be very helpful to me!
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jlgui.basicplayer.BasicController;
import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerEvent;
import javazoom.jlgui.basicplayer.BasicPlayerException;
import javazoom.jlgui.basicplayer.BasicPlayerListener;
public class MP3Player implements BasicPlayerListener, Runnable {
public String streamurl;
public BasicController playerControl;
private BasicPlayer player;
private volatile boolean shouldPlay = true;
#Override
public void run() {
while (true) {
if (shouldPlay) {
player();
}
}
}
public void start() {
new Thread(this).start();
}
public void pause() {
shouldPlay = false;
try {
playerControl.stop();
} catch (BasicPlayerException ex) {
Logger.getLogger(MP3Player.class.getName()).log(Level.SEVERE, null,
ex);
}
}
public void play() {
shouldPlay = true;
}
public MP3Player(String givenStreamurl) {
streamurl = givenStreamurl;
}
public void player() {
shouldPlay = false;
player = new BasicPlayer();
playerControl = (BasicController) player;
player.addBasicPlayerListener(this);
try {
try {
playerControl.open(new URL(streamurl));
} catch (MalformedURLException ex) {
System.out.println("aaa");
}
playerControl.play();
playerControl.setGain(0.85);
playerControl.setPan(0.0);
} catch (BasicPlayerException ex) {
}
}
}
error message
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at javazoom.jlgui.basicplayer.BasicPlayer.<clinit>(Unknown Source)
at MP3Player.player(MP3Player.java:57)
at Main.main(Main.java:6)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
line 57 is
player = new BasicPlayer();
thanx!
As the exception clearly says classnotfoundexception, which means that it cannot find the class org.apache.commons.logging.LogFactory in the classpath.
Download the jars from commons-logging and put it in your classpath.
This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 8 years ago.
when i run my java application with eclipse i don't get any problem but when i run it with the command prompt i get NoClassDefFoundError.
C:\Windows\System32>cd C:\Users\Caco\workspace\Bisquit_server\bin\bisquit_server
C:\Users\Caco\workspace\Bisquit_server\bin\bisquit_server>java ReceiveMsg
Exception in thread "main" java.lang.NoClassDefFoundError: ReceiveMsg (wrong nam
e: bisquit_server/ReceiveMsg)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
C:\Users\Caco\workspace\Bisquit_server\bin\bisquit_server>
this is my java code:
package bisquit_server;
import java.io.*;
import java.net.*;
public class ReceiveMsg {
private ServerSocket server_socket;
private Socket socket;
ObjectInputStream input_stream;
private static String msg;
public ReceiveMsg() throws IOException, ClassNotFoundException{
try
{
server_socket=new ServerSocket(50000,1);
while(true){
listen();
System.out.print("Server is ready to connect to a client");
createStreams();
initProcessing();
}
}
finally {close();}
}
private void listen() throws IOException{
socket= server_socket.accept();
ReceiveMsgTh rmth = new ReceiveMsgTh();
Thread t = new Thread(rmth);
}
private void createStreams() throws IOException{
input_stream= new ObjectInputStream(socket.getInputStream());
}
private void initProcessing() throws ClassNotFoundException, IOException{
msg = "";
msg = (String)input_stream.readObject();
}
private void close() throws IOException{
if (input_stream!=null && socket != null){
input_stream.close();
socket.close();
}
}
public static void main(String[] args) //throws ClassNotFoundException, IOException
{
try {
new ReceiveMsg();
}
catch(ClassNotFoundException | IOException e){}
new Store().StoreMsg(msg,new String[]{"0987654321,"1234567890"},"1234567890");
}
}
package bisquit_server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ReceiveMsgTh implements Runnable {
private ServerSocket server_socket;
private Socket socket;
ObjectInputStream input_stream;
public void StartProcess() throws IOException, ClassNotFoundException{
try
{
server_socket=new ServerSocket(50000,1);
while(true){
listen();
createStreams();
initProcessing();
}
}
finally {close();}
}
private void listen() throws IOException{
socket= server_socket.accept();
}
private void createStreams() throws IOException{
input_stream= new ObjectInputStream(socket.getInputStream());
}
private void initProcessing() throws ClassNotFoundException, IOException{
String msg = "";
msg = (String)input_stream.readObject();
new Store().StoreMsg(msg,new String[]{"0987654321","1234567890"},"1234567890");
}
private void close() throws IOException{
if (input_stream!=null && socket != null){
input_stream.close();
socket.close();
}
}
#Override
public void run() {
// TODO Auto-generated method stub
try {
StartProcess();
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
}
}
}
package bisquit_server;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Store {
public void StoreMsg(String msg, String[] mobileNumbers, String whoWrite){
File f = new File("C:\\Users\\Caco\\workspace\\Conversations\\"+mobileNumbers[0]+mobileNumbers[1]+".txt");
try (
BufferedWriter bw = new BufferedWriter(new FileWriter(f,true));
)
{
bw.write(whoWrite);
bw.write(" ");
bw.write(msg);
bw.write("\r\n");
}
catch(IOException e) {
System.out.print("Error writing file");
}
}
}
I'm really frustrated because it's three days that i try to fix it without succes!
Can you help me?
Thankyou
Just
cd ..
java bisquit_server.ReceiveMsg
Remember, you don't execute files with the java command, but classes. You need to give the fully qualified class name to the java command.
Also, it mmust be possible to find the class file that contains the class. This is done via the so-called class-path, which is just the current directory when you don't give one. So, to find bisquit_server.ReceiveMsgjava will look up a directory bisquit_server/ in the class path and in that directory, it will look for the ReceiveMsg.class file.
This way, you can run your program from a different location:
cd /temp
java -cp C:\Users\Caco\workspace\Bisquit_server\bin bisquit_server.ReceiveMsg
run javac command first to create .class files. after that run with java command
javac ReceiveMsg.java
and after that
cd..
java bisquit_server.ReceiveMsg
I want to build a RMI server, I tried like this
package first_project;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class Server extends UnicastRemoteObject implements ServerInterface {
// list for know users
protected ArrayList<ClientInterface> clients = new ArrayList<ClientInterface>();
public Server() throws RemoteException {}
//logged in clients get a notification that a new user has joined the chat
// remote reference to the new client is added to the ArrayList.
public void login(ClientInterface client, String nickname) throws RemoteException {
broadcastMessage("--> " + nickname + " is entering the chatroom", "");
clients.add(client);
}
// used for broadcasting an incoming message
// and the nickname of its sender to all currently logged in clients .
//remote call of the method getMessage which is
public void broadcastMessage(String message, String nickname) throws RemoteException {
for (int i = 0; i < clients.size(); i++) {
ClientInterface c = clients.get(i);
try {
c.getMessage(message, nickname);
} catch (RemoteException e) {
logout(c);
i = i - 1;
} }}
//remove user
public void logout(ClientInterface client) {
clients.remove(client);
}
public static void main(String[] args) {
try {
Naming.rebind("Server", new Server());
System.out.println("Server is ready");
} catch (Exception e) {
e.printStackTrace();
}
}
}
but i dont know why I got this exception:
java.rmi.ConnectException: Connection refused to host: 192.168.1.3; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at first_project.Server.main(Server.java:47)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
... 6 more
what am I doing wrong please?
thanks in advance
Have u started the registry. Try it after starting the registry
start rmiregistry for windows
rmiregistry & for Linux/Unix
Just had a question about an odd runtime error that I'm having. I'm working with a JSON server that responds with a JSON object when prompted by an outside source. As of right now, however, I'm simply trying to get the JSON object up and running before sending it out. My code compiles without specifying any classpath, but when it comes to runtime, it throws a NoClassDefFoundError as seen here:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at ExampleServer.sendData(ExampleServer.java:76)
at ExampleServer.runServer(ExampleServer.java:30)
at ExampleServer.main(ExampleServer.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 15 more
My code follows here:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.lang.*;
import net.sf.json.*;
public class ExampleServer{
private DataOutputStream output;
private DataInputStream input;
private ServerSocket server;
private Socket connection;
public static void main(String[] args){
new ExampleServer().runServer();
}
public void ExampleServer(){
System.out.println("Server object created");
}
public void runServer(){
try{
server = new ServerSocket(12345,100);
while(true){
try{
waitForConnection();
getStreams();
processConnection();
sendData("Text");
} catch(Exception e){
System.out.println("Server Terminated Exception.");
} finally {
closeConnection();
}
}
} catch(Exception e){
e.printStackTrace();
}
}
private void waitForConnection(){
try{
System.out.println("Waiting for connection.");
connection = server.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
} catch (IOException e){
System.out.println(e);
}
}
private void getStreams() throws IOException{
output = new DataOutputStream(connection.getOutputStream());
input = new DataInputStream(connection.getInputStream());
System.out.println("Got streams");
}
private void processConnection() throws IOException{
sendData("This is a test message");
}
private void closeConnection(){
try{
if(output != null && connection != null){
sendData("SEVER>>> TERMINATE");
output.close();
connection.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
private void sendData(String message){
try{
JSONObject g = new JSONObject();
g.put("message", message);
System.out.println(g.toString());
} catch (Exception e){
System.out.println("Error writing object");
}
}
}
and the JSON folder is in the same directory as ExampleServer.java, with the JSON classes residing in net/sf/json as specified in my imports. Why is it not working until runtime? Is my classpath not right for some reason? What would be the appropriate syntax for including a necessary classpath?
Thanks in advance
From json-lib homepage:
Json-lib requires (at least) the following dependencies in your
classpath:
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6
That your implementation compiles only shows that the compiler can find the right classes and methods. The implementation of those classes and methods may however depend on other libraries.
More information on project dependencies can be found here.