I am building an MP3 player in java and the script I have written can play an mp3 in main but when I put the same code in a subroutine in a different java file, it fails.
Here is the script when its in the main (StandAlone.java):
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.AdvancedPlayer;
import java.io.FileInputStream;
import java.io.IOException;
public class Stand_Alone {
public static void main(String[] args) {
String mp3FilePath = "song.mp3";
try (FileInputStream fileInputStream = new FileInputStream(mp3FilePath)) {
AdvancedPlayer player = new AdvancedPlayer(fileInputStream);
player.play();
} catch (JavaLayerException | IOException e) {
e.printStackTrace();
}
}
}
Here is the code as a main and subroutine (Main.java and SongPlayer.java):
public class Main {
public static void main(String[] args) {
SongPlayer.load();
}
}
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.AdvancedPlayer;
import java.io.FileInputStream;
import java.io.IOException;
public class SongPlayer {
public static void load() {
String mp3FilePath = "song.mp3";
System.out.println(mp3FilePath);
try (FileInputStream fileInputStream = new FileInputStream(mp3FilePath)) {
AdvancedPlayer player = new AdvancedPlayer(fileInputStream);
player.play();
} catch (JavaLayerException | IOException e) {
e.printStackTrace();
}
}
}
I am using Windows, I run it in the command prompt with the commands:
javac -cp javazoom_1.0.1.jar SongPlayer.java Main.java
java Main
The two scripts, the song and the javazoom jar are all in the same folder.
The result of running the command is:
Exception in thread "main" java.lang.NoClassDefFoundError: javazoom/jl/decoder/JavaLayerException
at Main.main(Main.java:4)
Caused by: java.lang.ClassNotFoundException: javazoom.jl.decoder.JavaLayerException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 1 more
The question is, why is java not able to see javazoom package when commands are launched from a subroutine, when it is able to see it from main in the stand alone case.
How can I get java to see javazoom from the subroutine?
Please help me, this is driving me nuts and halting my progress. I could put everything in one big java file but that's messy and confusing.