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.
Related
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);
}
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.
I built a java program using IntelliJ. This program loads media files like an image (.jpg) and audio (.wav).
When i run my project inside of the IDE, it works fine.
But when i generate a .jar file of my project, the image and audio files will not load.
When i try to load the .wav audio file, i get a FileNotFoundException
When i try to load the .jpg file, i get an IIOException: Can't read the input file!
I'm loading the image like this:
image = ImageIO.read(ImageScreen.class.getResourceAsStream("/quiz/resources/images/image_1.jpg"));`
When i inspect the top of the stack trace in the debugger, i can see: javax.imageio.ImageIO.read(Unkown Source).
In other parts of my application, i'm able to load text files from a similar directory like this and it works in the .jar file too:
reader = new BufferedReader(
new InputStreamReader(
WordScreen.class.getResourceAsStream(
"/quiz/resources/words/wordlist.txt")));`
I only have one package called quiz in my project and i don't understand why this isn't working, as i am specifying an absolute path.
I've looked at other questions such as this one, but in my opinion, i'm doing what is being suggested.
Thank you everyone for your help. I have managed to find a fix for my problem.
I have changed this:
image = ImageIO.read(ImageScreen.class.getResourceAsStream("/quiz/resources/images/image_1.jpg"));
To this:
image = ImageIO.read(this.getClass().getResourceAsStream("/quiz/resources/images/image_1.jpg"));
I am now retrieving the .wav in the same way and it is working:
this.getClass().getResourceAsStream("/quiz/resources/audio/audio1.wav");
try with getClassLoader()
this.getClass().getClassLoader().getResourceAsStream...
I'm making a rather big program in java. It has lots of files, including images, audio, and text files. When I'm running from Eclipse, the code I use works fine. However, I always have trouble gaining access to them from a .jar file that's on its own.
I'll give you an example of how I'm coding this.
For the BufferedImage, I have it this way:
File bgImgFile = new File("images/nature.jpg");
BufferedImage bgImg = ImageIO.read(bgImgFile);
For the AudioInputStream, I have:
File soundFile = new File("audio/two.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
And for the text file, I have:
FileReader reader = new FileReader("scripts/0.txt");
This all works fine for when I'm in Eclipse. However, I'm thinking about what I'll want to do for distribution. I want to have a .jar file that can run anywhere, and doesn't need to be in the same directory as those folders containing the files. I've done this before and it's worked fine.
This is what I tried to do for the image:
URL bgFileUrl = MainClass.class.getClassLoader().getResource("images/nature.jpg");
File bgImgFile = new File(bgFileUrl.getFile());
BufferedImage bgImg = ImageIO.read(bgImgFile);
I did similar things for the audio and text file. However, even when I tried in different combinations of having the files themselves in the same directory, having a slash in front of the plain file name, etc., nothing worked. I want to be able to include these files in the .jar file so I don't have to run the .jar from that specific folder.
One thing I don't understand is that, when I opened up the .jar file made using my original code, the images, audio, and scripts folders were in there and they had my files. They were there. However, when I tried to run it, I got an IIOException ("Can't read input file!") and a NullPointerException, even though I was using the same code as before.
How do I make it so the files are included in the .jar file so it can run on its own?
Thanks for the help!
Can you try below code
URL audioFileUrl = MainClass.class.getClassLoader().getResource("audio/two.wav"");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(audioFileUrl);
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?