Sound playing in netbeans but not jar - java

This snippet of code is supposed to play a short beep after the method is executed. Which it is doing inside netbeans. But when I use netbeans to build an executable Jar file it gives me a java.Lang.NullPointerException. Any ideas?
public void playSound() {
try {
AudioStream as = new AudioStream(ClassLoader.getSystemClassLoader().getResourceAsStream("resources\\beep-2.wav"));
AudioPlayer.player.start(as);
} catch (Exception e) {
e.printStackTrace();
}
}

Use a forward slash; backslash is Windows-specific and will only work when you're using an exploded layout.

change the code into the following it will surely work..
public void playSound() {
try {
AudioStream as = AudioSystem.getAudioInputStream(this.getClass().getResource("resources\\beep-2.wav"));
Clip clip = AudioSystem.getClip();
clip.open(as);
clip.start( );
}
catch (Exception e) {
e.printStackTrace();
}
}

It is not able to find your audio file. Make a resources folder in the directory where you jar is kept and keep the audio file in that folder.
Alternatively you can give an exact path in your program. e.g. C:\resources\beep-2.wav

Related

File not found java ini4j

I am trying to write something to a ini file using ini4j.
When i call the store() method it throws a FileNotFound exception even though it is in my project directory.
Maybe i did something wrong with my code?
Main:
public class Main {
public static Wini ini = null;
public static void main(String[] args) {
Config conf = new Config();
try {
conf.setMultiOption(true);
ini = new Wini();
ini.setConfig(conf);
ini.load(new File("apikeys.ini"));
} catch (InvalidFileFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
The file where i attempt to write and store the data:
if (KeyEndpoint.isValid(apikey)) {
Main.ini.put(apikey, Main.ini.get("Apikey"));
try {
Main.ini.store();
} catch (IOException e) {
channel.sendMessage("Invalid api key.").queue();
}
} else {
channel.sendMessage("API Key is invalid.").queue();
}
Any help is appreciated, I at least want to know what I am doing wrong.
Thanks!
It has to do with relative paths. Try changing the filename in Main for the absolute path, something like "/tmp/apikeys.ini", to check that your code works correctly. If that works, then you can now change it to either something like "../../directory/filename" or something relative to where you know you're executing your code from. Get familiar with your java path environment variables such as JAVA_HOME and all to get it to something more permanent and portable.

Files.copy returns empty file when jar is exported

The file downloads properly in eclipse however when i export the jar it always downloads a blank exe. Can anyone help?
public static void downloadAndRunFile(final URL from, final File to) throws Exception {
try (final InputStream in = from.openStream()) {
Files.copy(in, to.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
Desktop.getDesktop().open(to);
}
Actual code being ran
String bub = "https://a.coka.la/bnH6Vg.exe";
try {
Pandora.downloadAndRunFile(
new URL(bub),
File.createTempFile("feelthevluci", ".exe"));
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
The URL in your code seems to return a 404.
I changed it to something that I know works and is safe, and that works both in the IDE and in a jar file.
Check the URL via curl, browser, or other tool to make sure it is working.

Issue with Opening of PDF file in Java

I tried to use the following code for opening of the PDF file, but its not working. I tried to use the run time but still not opening. Even after debugging it is invoking the open() w/o any exception still, no sign of the pdf. What Have i done wrong?
#Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_F1)
{
if (Desktop.isDesktopSupported())
{
try
{
File myFile = new File("Doc.pdf");
Desktop.getDesktop().open(myFile);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
});
Got the problem, I had a problem with my pdf viewer. Had to re-install it, now the code works just great.

Trying to play sound in Java: NullPointerException

I'm trying to play sound in Java but it doesn't work and I got error message.
Here is my Code
public class PlaySoundClip extends JFrame {
public PlaySoundClip() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Test Sound Clip");
this.setSize(300, 200);
this.setVisible(true);
try {
URL url = this.getClass().getClassLoader().getResource("click.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);//Line 27
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY); // repeat forever
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new PlaySoundClip();//Line 44
}
}
And I get this error message then I'm not here any sound! How to fix this problem?
Exception in thread "main" java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidiFileReader.java:205)
at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:836)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:174)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1145)
at playsoundclip.PlaySoundClip.<init>(PlaySoundClip.java:27)
at playsoundclip.PlaySoundClip.main(PlaySoundClip.java:44)
Reference from Playing Sound.
Something is wrong with click.wav, it was not found on the classpath, so url became null, hence the NullPointerException.
You should put the click.wav on your classpath, so the program will find it. The most straightforward way is putting into the root folder of the jar file.
Most likely in your build phase there is a list of "resources" that are copied from source tree to the output compiled classes. Usually it is a string of regular expressions like ".txt;.json;…" In your case you need to add ".wav" to it or copy file by hand to the compiler output location.
Most IDEs (Eclipse|IteliJ IDEA) and some ant build scripts have provision for resource copying.

Getting images from a .jar file

I need to get res folder to compile when I export a executable jar file in eclipse also when I use the getClass().getResource() method it doesn't work.
Current reading image code
public Image loadImage(String fileName) {
return new ImageIcon(fileName).getImage();
}
Code that doesn't work
public Image loadImage(String fileName) {
return new ImageIcon(getClass().getResource(fileName).getImage();
}
I have now fixed the problem - this is the code that works
public BufferedImage loadImage(String fileName){
BufferedImage buff = null;
try {
buff = ImageIO.read(getClass().getResourceAsStream(fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return buff;
}
The value of fileName is just an image name eg
BufferedImage img = loadImage("background.png");
Thank you all for your help.
Either:
your path is wrong, you should get an error message if so, check it to see what path it actually tries, might not be what you think. Or debug and see what path it tries or even just print the path it tries.
or
it's not in the jar. Check the jar file with a zip-program and/or rename it to have zip in the and open it to check that the file is really there.

Categories

Resources