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.
Related
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.
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 am making a sound class for my game and after trying and trying i cant seem to get rid of the NullPointerException. This is happening because I cant access a variable in a try/catch statement.
Here is the code:
package util;
import java.applet.*;
import java.net.URL;
public class Sound
{
private AudioClip audio;
private URL file;
public Sound(String srcfile)
{
try
{
this.file = new URL(srcfile);
}
catch(Exception e){}
this.audio = Applet.newAudioClip(file);
}
public void Play()
{
this.audio.play();
}
public void Loop()
{
this.audio.loop();
}
public void Stop()
{
this.audio.stop();
}
public AudioClip getAudio()
{
return audio;
}
public void setAudio(AudioClip audio)
{
this.audio = audio;
}
}
Here is the error(no longer getting):
Exception in thread "main" java.lang.NullPointerException
at sun.applet.AppletAudioClip.<init>(Unknown Source)
at java.applet.Applet.newAudioClip(Unknown Source)
at util.Sound.<init>(Sound.java:19)
at main.Blocks.run(Blocks.java:38)
at main.Blocks.main(Blocks.java:26)
After revising the old code her is the new code:
package util;
import java.applet.*;
import java.net.URL;
public class Sound
{
private AudioClip audio;
public Sound(String srcfile)
{
try
{
this.audio = Applet.newAudioClip(new URL("file://" + srcfile));
}
catch(Exception e)
{
Log.log(e.getMessage(), Log.ERROR);
e.printStackTrace();
System.exit(1);
}
}
public void Play()
{
this.audio.play();
}
public void Loop()
{
this.audio.loop();
}
public void Stop()
{
this.audio.stop();
}
public AudioClip getAudio()
{
return audio;
}
public void setAudio(AudioClip audio)
{
this.audio = audio;
}
}
I am calling
Play();
but nothings happening
Here is how I'm calling the method:
Sound snd = new Sound("res/dev/sound.wav");
snd.Play();
Any help would be appreciated.
It doesn't look like from the way your class is designed that there's any need for the file variable to exist outside the constructor. Something like this would probably serve well:
public Sound(String srcfile) {
try {
this.audio = Applet.newAudioClip(new URL(srcfile));
}
catch(Exception e){
//at least print the stack trace
e.printStackTrace();
//do some proper exception handling that makes sense for you app!
}
}
I'm writing a small app to play a shoutcast stream, and I am using javazoom.jl.player.Player to do this. Here is my code:
package music;
import java.io.InputStream;
import java.net.URL;
import javazoom.jl.player.Player;
class audiostream extends Thread {
private Player mediafilePlayer;
private volatile boolean shouldPlay = true;
#Override
public void run() {
while (true) {
if (shouldPlay) {
player();
}
}
}
public void player() {
try {
URL mediafile = new URL("http://hi1.streamingsoundtracks.com:8000/;");
InputStream stream = mediafile.openStream();
mediafilePlayer = new Player(stream);
mediafilePlayer.play();
} catch (Exception e) {
System.out.println(e);
}
}
public void pause() {
shouldPlay = false;
mediafilePlayer.close();
}
public void play() {
shouldPlay = true;
}
}
This works perfectly fine on my Mac and I can hear the stream. However on Windows when I try to run this I get the error "java.io.IOException: Invalid Http response". I believe this is because SHOUTcast returns icy 200 ok headers wherein something on Windows must want it to return http headers. I can't seem to find how to make it accept these different headers on windows using javazoom Player.
I ended up solving this issue by using BasicPlayerListener instead. I replaced the code in my question with the following:
package music;
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 audiostream implements BasicPlayerListener, Runnable {
public String streamurl;
public BasicController playerControl;
private volatile boolean shouldPlay = true;
#Override
public void run() {
while (true) {
if (shouldPlay) {
player();
}
}
}
// ** RUN ONCE TO START THREAD
public void start() {
new Thread(this).start();
}
// ** RUN TO PAUSE/STOP THE PLAYER
public void pause() {
// set play bool to false
shouldPlay = false;
// stop player
try {
playerControl.stop();
} catch (BasicPlayerException ex) {
Logger.getLogger(audiostream.class.getName()).log(Level.SEVERE, null, ex);
}
}
// ** RUN TO PLAY
public void play() {
shouldPlay = true;
}
// construct
public audiostream(String givenStreamurl) {
// assign the radio url
streamurl = givenStreamurl;
}
// OPENS UP THE SHOUTCAST STREAM
public void player() {
// dont allow multiple runnings of this
shouldPlay = false;
// start stream
try {
BasicPlayer player = new BasicPlayer();
playerControl = (BasicController) player;
player.addBasicPlayerListener(this);
try {
playerControl.open(new URL(streamurl));
} catch (MalformedURLException ex) { }
playerControl.play();
} catch (BasicPlayerException ex) { }
}
#Override
public void opened(Object o, Map map) {
//System.out.println("opened : "+map.toString());
}
#Override
public void progress(int i, long l, byte[] bytes, Map map) {
//System.out.println("opened : "+map.toString());
}
#Override
public void stateUpdated(BasicPlayerEvent bpe) {
//System.out.println("opened : "+bpe.toString());
}
#Override
public void setController(BasicController bc) {
//System.out.println("opened : "+bc.toString());
}
}
I'm currently developing a system that loads classes via rmi. This system uses a classloader that communicates with the server in order to get the classes. The code is as follows.
Server:
import rocks.squareRock;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject
implements RemInterface {
public Server() throws RemoteException {
super();
}
public static void main(String argv[]) {
try {
Server serv = new Server();
Naming.rebind("RockServer", serv);
} catch (Throwable t) {
t.printStackTrace();
}
}
public Class<?> getRockClass(String type) {
if (type.equals("squareRock"))
return squareRock.class;
else
return null;
}
}
Client:
import rocks.Rock;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class Client {
RemInterface reminterface = null;
RockLoader rl = null;
public Client() {
String strName = "rmi://127.0.0.1/RockServer";
try {
reminterface = (RemInterface) Naming.lookup(strName);
rl = new RockLoader(reminterface);
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
loadRock("squareRock");
}
public Rock loadRock(String rock) {
try {
return (Rock) rl.loadClass(rock, false).newInstance();
} catch (Throwable t) {
return null;
}
}
}
Interface:
public interface RemInterface {
public Class<?> getRockClass(String type) throws RemoteException;
}
RockLoader:
import java.io.Serializable;
public class RockLoader extends ClassLoader implements Serializable {
private RemInterface reminterface = null;
public RockLoader(RemInterface reminterface) {
super();
this.reminterface = reminterface;
}
#Override
protected synchronized Class<?> loadClass(String className, boolean resolve)
throws ClassNotFoundException {
try {
return reminterface.getRockClass(className);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
The error I'm getting with this is (client-side):
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: SquareRock
This confuses me, as I'm not unmarshalling a SquareRock instance, but a Class. The only thought I have is that my classloader might be wrong.
It doesn't matter whether it's a Class or an object. The receiving JVM must have that class in its classpath, unless you are using the RMI codebase feature. What you are doing is basically trying to implement the codebase feature yourself. You can't do that.