Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

javazoom mp3 help needed

807591Jun 14 2008 — edited Jun 15 2008
Hello. I have downloaded this Javazoom Jalayer mp3 program from here to use with my programs:
http://www.javazoom.net/javalayer/sources.html
I wanted to use it to play mp3 files. I made a program (below) and it's working fine in eclipse, but when I export it to a JAR, it doesn't work.

Below is a piece of code that I was using to test the javazoom thing. It will play the mp3 music.mp3 from the file Music in the same directory. It seems it stops working at "player = new Player (bis);" when it's a jar, buts it works fine in eclipse. I'm thinking I didn't export the player properly with eclipse.

So far, in order to get it to work in eclipse, all I did was add an External JAR to my java build path, and selected the downloaded jl1.0.jar. I don't think I touched anything else.

After that, I created the MusicTest program and exported my project into a jar. However, when I execute the jar, the music doesn't play.

I'm thinking that maybe theres some classpath thing that I didn't do, but I thought that the External Jar creation in eclipse was all I needed.

Can somebody help me with this problem?
import java.awt.Frame;
import java.awt.TextArea;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javazoom.jl.player.Player; // import javazoom player

public class MusicTest
{
    static Frame test;
    static TextArea text = new TextArea (20, 20);
    static Player player;
    static WindowListener musicWindow = new WindowAdapter ()  // add a windowlistener to window so that the closed button can be pressed
    {
        public void windowClosing (WindowEvent ev) 
        {
            System.exit (1);
        }
    }


    ;

    public static void main (String [] args)
    { // entry point of program
        test = new Frame ();
        test.add (text);
        test.pack ();
        test.setVisible (true);
        text.append ("Music\n");
        test.addWindowListener (musicWindow);
        String filename = "Music/music.mp3";
        try
        {
            FileInputStream fis = new FileInputStream (filename);
            text.append ("fileinputstream created\n");
            BufferedInputStream bis = new BufferedInputStream (fis);
            text.append ("bufferedimputstream created\n");
            player = new Player (bis); // <-- When i create a jar, the program stops here
            text.append ("player created\n");
        }
        catch (Exception e)
        {
            System.out.println ("Problem playing file " + filename);
            System.out.println (e);
            text.append ("Problem playing file " + filename);
            text.append (e.getMessage ());
        }
        try
        {
            text.append ("music on\n");
            player.play ();
        }
        catch (Exception e)
        {
            System.out.println (e);
            text.append (e.getMessage ());
        }
    }
}
Edited by: Integrate on Jun 13, 2008 9:27 PM

Comments

807591
What do you mean by saying the program "stops"?

Is there any error message printed to System.out?
807591
I mean the system stops as in it doesn't execute here:
text.append ("bufferedimputstream created\n");
player = new Player (bis);
text.append ("player created\n");
It prints out "bufferedimputstream created", but does not print out "player created" when I run the jar.

It does work in eclipse, where no error message is printed at all since it's working. The problem only arises in the Jar, which is why I created the text area to display any error messages. However, no error has been displayed at all either. It just stops at player = new Player (bis);, leading me to believe that the Player class hasn't been exported or linked correctly (I'm not sure about that, I don't have much experiance with classpaths or exporting, so I might be completely off). However, I was thinking that since it works in eclipse, it should work in the jar because i thought javazoom would be exported with everything when I export the project.

Was I supposed to do anything with the environment variables to make javazoom work?
807591

It prints out "bufferedimputstream created", but does not print out "player created" when I run the jar.

These are actually appended to the TextArea rather than printed to System.out.

The problem only arises in the Jar, which is why I created the text area to display any error messages. However, no error has been displayed at all either

I think it would be more straightforward to read the error messages from the console. There is no reason why you can't do this (or - to put it another way - no reason to create the text area and append stuff to it from the main() thread), just invoke java from the command line:

java -jar MyJar.jar

where MyJar.jar is the jar file you exported. This way the messages from the program will be sent to the console and you can read them.

My guess would be that "Music/music.mp3" doesn't exist. But that's only a guess until we see the output produced by the program.

(The relative name "Music/music.mp3" refers to a file somewhere where you keep your eclipse projects. But after you export the jar file it will refer to somewhere else depending on how you launch the application.)

807591
Oh, so I guess thats why I never saw an error. I'm not at all experianced with running java from the command prompt.

Anyways, here is the error I get:

Exception in thread "main" java.lang.NoClassDefFoundError: javazoom/jl/player/Pl
ayer
at MusicTest.main(MusicTest.java:42)
Caused by: java.lang.ClassNotFoundException: javazoom.jl.player.Player
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 1 more

It seems that the javazoom player was not found, but I thought exporting it would cause the jar to include all the necessary files within it so that it can be standalone. I'm guessing that I'm not setting something right in eclipse, but I've already added it to my external jars, so I don't know what's going on.
807591

It seems that the javazoom player was not found, but I thought exporting it would cause the jar to include all the necessary files within it so that it can be standalone

The third party classes are not included when you export the project as a jar archive. (This is by design as you may want to use them in multiple projects, be able to swap them for newer versions etc. Also there may be legal complications.). Rather you make sure that the java executable knows where to locate classes at the time you run it. This is where the classpath you were asking about comes in: it is the set of places the java executable should look for classes.

This can be done in two ways:

(1) Either - specify the required locations when you run the application

java -cp .;c:\jars\jl1.0.jar MusicTest

This will look for classes in the current directory (.) and in the jl.1.0.jar archive. Hopefully it will find everything it needs!

(2) Or - specify the classpath as part of the jar archive itself. (This might make more sense in your case where you want the archive to act like a stand alone application.) The jar archive includes a special entry called the "manifest" which records information that will be useful when using the archive, including the classpath to be used when it is run as an application.

When you exported the project from Eclipse, the fourth (?) screen of the wizard - where you entered the main class - allows you to save the manifest in the workspace. This option appears near the top of the dialog box. By choosing it you can save the generated manifest as part of your eclipse project. The manifest appears there after you have exported the project. Then you can open it. It looks something like this:

Manifest-Version: 1.0
Main-Class: MusicTest

(A) Add a line to the manifest so that it can find the third party classes it needs:

Manifest-Version: 1.0
Main-Class: MusicTest
Class-Path: c:\jars\jl1.0.jar

(There's a blank line at the end of this file.)

(B) Export the project again. This time - at the same place in the wizard - tick "Use existing manifest from workspace" choosing the manifest that was previously created and altered.

Now the exported jar file "knows about" the correct classpath (location of required classes) and the main class to run, soyou can run it with the simpler command

java -jar MyJar

or just double click its icon if your desktop supports that.

-----

(I haven't actually checked the Class-Path line in step (A), but it works on Linux using

Class-Path: /home/pbrockway/Desktop/jl1.0.jar

There is a discussion about using the manifest as part of deploying applications with jar files in [Sun's Tutorial|http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html] and someone may be able to point to towards a step-by-step guide to the Eclipse exporting process.

What I called step (A) need only be done once, and isn't really necessary: you can create the manifest as a simple text file.)

807591
Thanks for the help, but I'm still having a little trouble with the last bit.

I have placed my javazoom stuff in the C drive under jars, and I can get my jar to work with this:
java -cp .;c:\jars\jl1.0.jar MusicTest
However, the manifests don't seem to want to cooperate.

I have created the manifest using the eclipse, and I added the class path line (with the blank line still at the end). However, when I export the jar again and use that manifest in the wizard, and I double click the jar file, I get an error:

Could not find the main class: MusicTest. Program will exit.

I ran it again in the command prompt using jar -jar MyJar.jar and I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: MusicTest
Caused by: java.lang.ClassNotFoundException: MusicTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: MusicTest. Program will exit.

Which is where I get stuck. I tried erasing the Class-Path: c:\jars\jl1.0.jar line from the manifest and exporting again, and it creates the jar file which I can run, but has the problem that I started with. This means that the manifest is created properly, but something could be wrong with the line Class-Path: c:\jars\jl1.0.jar
807591

I double click the jar file, I get an error:

Could not find the main class: MusicTest. Program will exit.

I ran it again in the command prompt using jar -jar MyJar.jar and I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: MusicTest

These cases are similar, and in both it is the MusicTest that can't be located. (I guess you meant "java -jar MyJar.jar"). It is looking for an entry - MusicTest.class - right at the top of the jar archive. You can check that it's in the right place with:

java tf MyJar.jar

which will list the contents of the archive.

You are expecting to see:

META-INF/MANIFEST.MF
MusicTest$1.class
MusicTest.class

There may be other entries, but these won't stop the MusicTest class being located. If these two MusicTest class entries don't appear as shown, there is something wrong with your export settings.

It is also possible that you have made some accidental change to the source code. The main() method must be declared exactly as you have it in your original post: slight changes will result in something that will still compile and export, but won't run.

807591
OK, I installed the JDK onto a Windows machine so I could see what you are seeing.

It turns out that you can't specify the drive letter as part of the CLASS-PATH: line. From the command line, this is what I saw:
C:\Documents and Settings\d6\My Documents>dir /b c:\jars
jl1.0.jar

C:\Documents and Settings\d6\My Documents>dir /b
manifest.txt
Music
MusicTest.java
My Music
My Pictures

C:\Documents and Settings\d6\My Documents>dir /b Music
music.mp3

C:\Documents and Settings\d6\My Documents>type manifest.txt
MAIN-CLASS: MusicTest
CLASS-PATH: \jars\jl1.0.jar

C:\Documents and Settings\d6\My Documents>
C:\Documents and Settings\d6\My Documents>
C:\Documents and Settings\d6\My Documents>
C:\Documents and Settings\d6\My Documents>javac -cp .;c:\jars\jl1.0.jar MusicTest.java

C:\Documents and Settings\d6\My Documents>jar cmf manifest.txt MyJar.jar *.class

C:\Documents and Settings\d6\My Documents>java -jar MyJar.jar

C:\Documents and Settings\d6\My Documents>
The dir and type commands are just to show the location of the various files.

The last three commands are the important ones: they compile, jar and run the program respectively. At that point the program runs and the music is audible. The music also plays if I double click MyJar.jar. And if I move both MyJar.jar and the Music folder to some other location and double click the jar file. (This is because you have specified the music file relatively.)
807591
Thank you, I think its working now. I changed the class path line in the manifest to:
Class-Path: jl1.0.jar

and placed the library in the same place as my jar file. I was able to run the jar after that.

However, I think I have another problem. I origianally wanted my program to run on any system (which is why I wanted a jar stand alone). However, when I place my jar onto another computer, it would not run. It gives me a UnsupportedClassVersionError in the command prompt. This is because my other computer has jre1.5.0_02 while the eclipse on the computer that created the jar has a jre1.6.0_06 system library. I'm thinking since these two versions don't match, the jar wouldn't work on the other computer.

I guess this is kinda off from the original question, but is there a way to make this jar work on the previous versions of java? Also, does a computer need a java jre installed in order to open the jar? If so, I guess that woudn't really be stand alone...

I did some reasearch, and it said to change my compilance level in eclipse down (I did not change my system library). It does work, but then menubars actions don't work.
807591
Also, does a computer need a java jre installed in order to open the jar? If so, I guess that woudn't really be stand alone...
Yes - you need a JRE. That's just how Java works: one aspect of being "cross platform" is that each platform has its own runtime support (the Java Runtime Environment). This allows your program to do whatever it wants relying on the JRE to convert that into something that makes sense on the particular platform (Windows, Mac etc).
I did some reasearch, and it said to change my compilance level in eclipse down (I did not change my system library). It does work, but then menubars actions don't work.
You can either compile for an earlier runtime (as you suggest) or install a later runtime on the 1.5 machine. If only one machine is involved, then that would be the easiest option.

If you can isolate what it is that breaks when you try compiling it for 1.5, then it might be worth starting another thread to try and work around that.
807591
After looking around, I think its just easier to make a readme that says which version of java my program can run on.

Anyways, thanks for the help! My problems are pretty much resolved now. Your advice and guides were very clear. :D

Edit: being the first time on this forum, I don't know if it is custamary to select which post was correct, but I think I selected the wrong one (should be the one under the post I have selected). I can't seem to change it afterwards...

Edited by: Integrate on Jun 15, 2008 12:13 AM
807591
Anyways, thanks for the help! My problems are pretty much resolved now.
You're welcome.

I don't think the stars matter that much. The post you wanted to mark as "correct" I posted because I wanted to show how straight forward the compiling/jarring/running business is without Eclipse.
1 - 12
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 13 2008
Added on Jun 14 2008
12 comments
2,625 views