Trouble with playing .wav files from JAR - java

I'm having some strange trouble with playing a WAV file from a JAR.
First, I can simply read it using ClassLoader when working in Eclipse. Everything works fine there.
Then I export my project to a JAR file. But now it says that no such files exists although I can see it with an archivator; for non-WAV files it works fine.
When I decided to export it to an external directory first the WAV file exports corrupted (while other files are not damaged, again).
What should be my problem then? I've made a research, but I couldn't find anything related to any trouble with WAV files or heavy files.
How do I do it correctly and why does it work for small files but not the big ones (or sound files)?
Thank you for your awnsers.
EDIT:
The package explorer in Eclipse:
http://i.imgur.com/7TTyQit.png
The code used to actually get the file:
private static MusicPlayer music = new MusicPlayer(new InputStream[]{MusicPlayer.class.getResourceAsStream("default/Final.wav")}, new InputStream[]{});
Finally, the exception:
java.io.IOException: mark/reset not supported
at java.util.zip.InflaterInputStream.reset(Unknown Source)
at java.io.FilterInputStream.reset(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at ru.windcorp.game.music.Music.run(Music.java:31)
at ru.windcorp.game.music.MusicPlayer.playMenu(MusicPlayer.java:29)
at ru.windcorp.game.GameMain.main(GameMain.java:152)

Don't make your AudioInputStream from an InputStream. Make it from a URL.
(1) URL's can "see" into jars.
(2) getAudioInputStream(InputStream inputStream) likely runs a Markability or Resetability test (or both) on the audio file when it is converted to an InputStream. Most audio files seem to fail at this stage.
(3) getAudioInputStream(URL url) does NOT run a Markability test on the audio, thus circumventing the issue.
Check out the API for the different input parameters of this method, for the AudioSystem object.
Whoa! Why are you using com.sun.media.sound code? I think that has mostly been deprecated, hasn't it? SoftMidiAudioFileReader is no where to be found in the Java 7 SE API. A slew of people are using this and generating errors. Where did you get the suggestion to use this?

Related

Writing Information (and Image) to a Jar File from another Java Program

Possibly a duplicate, though I doubt so since I have not seen anything so far completely answering my criteria in a way that I can complete my program
Background
What I need is to access another jar, from a seperate jar, read and write files to that jar. So far what I have done is change the jar to a zip and then I can delete files, but the problem I am having is with writing files back in, specifically image files (.txt works perfectly fine)
Question
How do I write image files to a zip (that was originally a jar) from another java program (in the end product another jar)
Note
I have looked around and most sources say this is not possible, but those questions dealt with this during the running of a program, my special case is that the other program is not running, but in file format. All I want to do is write and image in and convert it back to a jar and not have any problems with running that jar in the end.
Thank you!
Use FileSystems to access, write and replace the contents of the jar file:
try (FileSystem fs = FileSystems.newFileSystem(Paths.get("path/file.jar"), null)) {
Files.copy(Paths.get("path/to/image"), // path to an external image
fs.getPath("image.jpg"), // path inside a jar file
StandardCopyOption.REPLACE_EXISTING);
}

Where to find file from instructions on how to use Tesseract API or is there another way to fix this error?

I'm just so frustrated. I have this personal project that my dad gave me so I could help out his company in my own way. I'm trying to use the Tesseract API to read words from images. I've never done anything like adding a library or anything technical so I followed these instructions to help me out:
https://tphangout.com/how-to-use-the-tesseract-api-to-perform-ocr-in-your-java-code/
https://dzone.com/articles/reading-text-from-images-using-java-1
I followed these instructions to the letter except for one portion (step 7; they ask to put liblept.dll file; after I extracted Tess4j I couldn't find that specific .dll file.) I keep getting an error.
I've already deleted my Netbeans Project and retried installing and following the same steps. I also even thought possibly it was the image file type so I switched between tiff, png, and jpg. Nothing.
The ImageReader class
import java.io.*;
//import java.util.*;
import net.sourceforge.tess4j.*;
public class ImageReader {
String filePath;
public ImageReader(String s) {
filePath = s;
}
public String getImageReadings() {
File imageFile = new File(filePath);
Tesseract instance = new Tesseract();
try {
String result = instance.doOCR(imageFile);
return result;
}
catch (TesseractException e) {
System.err.println(e.getMessage());
return "Error while reading image";
}
}
}
I expected it to spit out a Hello when it read the png I created with Hello typed into it. But it just gave me this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'libtesseract3051': Native library (win32-x86-64/libtesseract3051.dll) not found in resource path ([file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/commons-beanutils-1.9.2.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/commons-io-2.6.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/commons-logging-1.2.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/fontbox-2.0.9.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/ghost4j-1.0.1.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/hamcrest-core-1.3.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/itext-2.1.7.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/jai-imageio-core-1.4.0.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/jbig2-imageio-3.0.0.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/jboss-vfs-3.2.12.Final.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/jcl-over-slf4j-1.7.25.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/jna-4.1.0.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/jul-to-slf4j-1.7.25.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/junit-4.12.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/lept4j-1.6.4.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/log4j-1.2.17.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/log4j-over-slf4j-1.7.25.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/logback-classic-1.2.3.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/logback-core-1.2.3.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/pdfbox-2.0.9.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/pdfbox-tools-2.0.9.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/slf4j-api-1.7.25.jar, file:/C:/Users/kakas/OneDrive/Documents/Tess4J/lib/xmlgraphics-commons-1.5.jar, file:/C:/Users/kakas/OneDrive/Documents/NetBeansProjects/PDFSorter/build/classes/])
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:271)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
at com.sun.jna.Library$Handler.<init>(Library.java:147)
at com.sun.jna.Native.loadLibrary(Native.java:412)
at com.sun.jna.Native.loadLibrary(Native.java:391)
at net.sourceforge.tess4j.util.LoadLibs.getTessAPIInstance(LoadLibs.java:81)
at net.sourceforge.tess4j.TessAPI.<clinit>(TessAPI.java:42)
at net.sourceforge.tess4j.Tesseract.init(Tesseract.java:379)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:292)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:224)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:208)
at pdfsorter.ImageReader.getImageReadings(ImageReader.java:25)
at pdfsorter.PDFSorter.main(PDFSorter.java:19)
I'm kinda at a lost for what to do because I've only ever did mini projects like build a Calculator up until now. I think it's the issue with the dll file that I couldn't find but I'm not sure. Also just a recent update, for some reason it gives a different error with a different png from the same location. The second error is below:
[main] ERROR net.sourceforge.tess4j.Tesseract - I/O error reading PNG header!
javax.imageio.IIOException: I/O error reading PNG header!
javax.imageio.IIOException: I/O error reading PNG header!
at com.sun.imageio.plugins.png.PNGImageReader.readHeader(PNGImageReader.java:315)
at com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:654)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1256)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1614)
at javax.imageio.ImageReader.readAll(ImageReader.java:1066)
at net.sourceforge.tess4j.util.ImageIOHelper.getIIOImageList(ImageIOHelper.java:395)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:224)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:208)
at pdfsorter.ImageReader.getImageReadings(ImageReader.java:25)
at pdfsorter.PDFSorter.main(PDFSorter.java:19)
Caused by: javax.imageio.IIOException: Bad PNG signature!
at com.sun.imageio.plugins.png.PNGImageReader.readHeader(PNGImageReader.java:242)
... 9 common frames omitted
I'm unsure of how I can post the pngs I used because they are in my local drive but I'll see what I can do.
Update:
I finally figured out. Overall the issue lied within the explanation and instructions I used being too outdated. I fixed it when finding new tutorials.
One of the jar files I had forgotten to add to the library folders in my Netbeans Project were in the Tess4j’s dist folder. From https://sourceforge.net/projects/tess4j/files (which is where you download everything), you need to download both tess4j and lept4j projects. Then from their dist and lib folders after unzipping the folder, add all jars to your project’s library. Then, open the tess4j and lept4j from your IDE as a project and copy their source packages into your project’s source packages. Lastly, look for the .dll files in Tess4j and Lept4j (for each project there should be 2 .dll that just differ in 32bit vs 64bit) and copy one of each into your project’s source package labeled net.sourceforge.tess4j.

Trouble Understanding/Locating a Jar file (JIDT package) needed for Octave to Java Array Conversion

In this previous question, I was trying to rework some Matlab code and figure out a package called javaplex to be compatible with Octave; it uses Java, but is tooled for Matlab, hence that issue. Now in an interval of time, I was busy/running simulations, and hadn't gotten around to a final step - actually using the package, with most all of the difficulties worked out. It turns out that another step exists: I need to convert an Octave array to a Java array (although I'm not sure why this issue didn't come up in Matlab).
To do so, I have turned to this script, in which the comments indicate that when using it, it
Assumes the JIDT [Java Information Dynamics Toolkit] jar is already on the java classpath - you will get a java classpath error if this is not the case.
So I go to the JIDT GitHub page and download this package. Now I am not a very avid user of java, so I believe I am failing to see something fairly straightforward: I am not sure where the "JIDT jar" is that is referenced in the above block quote! I can't find such a particular jar file to put in Octave's java classpath. In this tutorial for JIDT, they say you need the "infodynamics.jar" file in the classpath (page 9). I'm not sure what jar file I should be looking for, and where. Any help understanding the nature, name and location of this jar file (within the infodynamics toolkit folder) would be appreciated!
As an inevitable follow-up question, because this will come up upon resolving this issue, I would like to clarify the following procedure is how to add a jar file to the Octave (static) java classpath (following this answer here, I wasn't sure if I was implementing correctly):
I create a file called "javaclasspath.txt" inside of the directory I use in Octave.
I enter the name of files as follows: "./path/to/your-file.jar"
I suppose my main issue here is where do I start the path (all the way back with "C:/..."?), and do I put this "javaclasspath.txt" file in the directory folder I will be using most of the time in Octave?
Edit: I cannot find "infodynamics.jar" as shown here:
The JIDT jar is named infodynamics.jar and it is located in the root of the downloads infodynamics-dist-1.4.zip file.

Playing an MP3 in a jar with mp3spi

I'm working on a Java project in Eclipse IDE. I want to deploy the project into a jar-file. Getting the audio to run is the part I have diffictuly with. I'm using the mp3spi library by javazoom for playing mp3 files. ( http://www.javazoom.net/mp3spi/sources.html )
I implemented my AudioPlayer almost like the sample on the javazoom website. ( http://www.javazoom.net/mp3spi/documents.html )
I could post the lengthy code here again, but it's almost the same with the difference that my AudioPlayer extends Thread so it can play independently. (and it works just fine in the development environment)
The problem I have is that I just can't get it to run when I export the project into a jar. I found already many questions and suggestions to this topic and I tried most of it. Sadly I still don't have a working solution. I'm sure it's out there but at this point I might miss the forest for the trees.
All the required libraries (jl1.0.1.jar, tritonus_share.jar and mp3spi1.9.5.jar) are in my jar and added to the classpath.
The curcial point of the code seems to be AudioInputStream (at line 10 in the sample) which takes a File to be created.
1st attempt - the code like suggested on javazoom.net:
File file = new File("src/resources/audio/test.mp3");
AudioInputStream ain = AudioSystem.getAudioInputStream(file);
What happens is:
Eclipse IDE:
Works fine, the mp3 plays.
Exported jar-File:
Jar works but mp3 does not play.
Exported jar-File opend with windows console:
Jar works but mp3 does not play.
Error messages: at audio.AudioPlayer.run(AudioPlayer.java:40)
java.io.FileNotFoundException: src\resources\audio\test.mp3
So I can't use „File“ in a jar because I'm not dealing with a file in the file system but a file inside my jar.
2nd attempt
URL url = ClassLoader.getSystemResource("resources/audio/test.mp3");
AudioInputStream ain = AudioSystem.getAudioInputStream(url);
What happens is:
Eclipse IDE:
Works fine, the mp3 plays.
Exported jar-File:
Jar works but mp3 does not play.
Exported jar-File opend with windows console:
Jar works but mp3 does not play.
Error messages: javax.sound.sampled.UnsupportedAudioFileException:
could not get audio input stream from input URL
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at audio.AudioPlayer.run(AudioPlayer.java:40)
I can't get an AudioInputStream from a URL? I don't know. So let's get to ...
3rd and 4th attempt
Attempt 3:
BufferedInputStream myStream = new BufferedInputStream(getClass().getResourceAsStream("/resources/audio/test.mp3"));
AudioInputStream ain = AudioSystem.getAudioInputStream(myStream);
Attempt 4:
BufferedInputStream myStream = new BufferedInputStream(ClassLoader.getSystemResourceAsStream("resources/audio/test.mp3"));
AudioInputStream ain = AudioSystem.getAudioInputStream(myStream);
Again both with the result playing in Eclipse IDE but not in the jar.
Exported jar-File opend with windows console:
Jar works but mp3 does not play.
Error messages: javax.sound.sampled.UnsupportedAudioFileException:
could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at audio.AudioPlayer.run(AudioPlayer.java:40)
I am really lost here. Any help would be much appreciated.
Related topics:
File path or file location for Java - new file()
Reading File In JAR using Relative Path
Java Audio Stream (mp3spi lib), UnsupportedAudioFileException
Jar with compressed music with java?
I used to export my project always as a jar and all images that I loaded in a similar manner would load properly while the audio file would not.
Now, that I tried to export my project as a runnable jar, it works just fine (attempt 4, maybe other attempts as well). The following question and answer explains the difference between jar and runnable jar.
Java Eclipse: Difference between exporting as a JAR and exporting as a Runnable JAR
How that manifest file impacts the proper loading of resources with
ClassLoader.getSystemResourceAsStream
is beyond me, but apparently it does.
Somebody might want to elaborate on those maifest files because I really can't. Or perhaps there are other differences between the export of a jar and a runnable jar.
Since I have a working solution I am done for now with this subject.

Running a Jar file alone

Hai
I have an application which is designed in netbeans6.0.1 where i need to display an image as an logo.My coding is as below
mLogo = new JLabel();
URL lUrlLogo = getclass().getResource("/com/images/image.gif");
Image lLogoimage = Toolkit.getDefaultToolkit().getImage(lUrlLogo);
ImageIcon licon = new ImageIcon(lLogoimage);
mLogo.setIcon(licon);
My code works fine when i execute it in netbeans but when I try to run my jar file which is created by netbeans in dist folder,it gives me an exception
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
Whats the problem with my jar
Can some one help me to run my jar
Thanking u
Your jar file will need to include the image.
See this tutorial or this one for instructions for how to do that. Basically if you include the file in a source folder (with the appropriate path) it should be okay.
If you think you've already got the image there, run
jar tvf file.jar
to show the contents. Make sure everything's where you expect it to be in terms of directories.
EDIT: As per the comments, the problem turned out to be due to case sensitivity.
Finding resources in a jar file is always case-sensitive, whereas loading them from a file system is only case-sensitive if the file system is. So if your file is image.GIF but you're looking for image.gif then it'll work when you're loading from the file system in Windows, but not in the jar file.
And you have checked that the JAR file contains the path "/com/images/image.gif"?
Also, I'm not really sure about the leading slash, I always used relative paths to access resources (but I guess it should work anyways).
As an example from class it.lapo.example.Main I use getClass().getResource("resources/logo.png") and in the JAR files are this way:
it/lapo/example/Main.class
it/lapo/example/resources/logo.png
I know... not much different from your own example, but it does work... so I guess yours is quite close to being correct too.

Categories

Resources