开发者

Problem Running Ant Script In Netbeans 6.1

开发者 https://www.devze.com 2023-02-12 08:56 出处:网络
I tried to import my ant script. And it successful. But when i compile it. I got this error: run-selected-file-in-src:

I tried to import my ant script. And it successful. But when i compile it. I got this error:

run-selected-file-in-src:
java.io.FileNotFoundException: ..\sounds\voice.wav (The system cannot find the path specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at com.sun.media.sound.WaveFileReader.getAudioInputStream(WaveFileReader.java:205)
        at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1162)
        at SimpleSoundPlayer.<init>(SimpleSoundPlayer.java:35)
        at SimpleSoundPlayer.main(SimpleSoundPlayer.java:12)
Exception in thread "main" java.lang.NullPointerException
        at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:89)
        at SimpleSoundPlayer.main(SimpleSoundPlayer.java:16)
D:\Windows\My Document\Latihan\Java\allsrc\ch04src\nbproject\ide-file-targets.xml:7: Java returned: 1
BUILD FAILED (total time: 0 seconds)

This is my folder structure

root
+-build(all *.class store here)
+-images
+-nbproject(netbeans create it)
+-sounds(voice.wav where i want to load)
+-src(all *.java - When i compile. All *.class will store to "build" folder)
+-b开发者_如何转开发uild.xml

My question is why the class file(SimpleSoundPlayer.class) didn't find "..\sounds\voice.wav". But when i tried it from command-line "java SimpleSoundPlayer" it fine.

Why this happen?


This happens because you use a relative path to load the file : ..\sounds\voice.wav. When you do this, your app is very fragile, because it must be launched from a specific directory to work correctly.

I suspect that NetBeans launches your app from the root folder instead of launching it from the src or build folder.

I would recommend to put the voice.wav in your sources, so that it's copied into the build directory, along with your class files. Then you may load the file using the classloader, thanks to a call to Class.getResourceAsStream().


I FIGURE IT OUT!!! Special Thx to "JB Nizet". Because him i know why it didn't find the wav file. So i just search how to change the default location(this case in root) netbeans to compile(I want netbeans to compile from "build" folder).

The key is in nbproject/ide-file-target.xml

root
+-build(all *.class store here)
+-images
+-nbproject(netbeans create it)
+-sounds(voice.wav where i want to load)
+-src(all *.java - When i compile. All *.class will store to "build" folder)
+-build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="../build" name="chap04 - Sound Effects and Music-IDE">
    <!-- TODO: edit the following target according to your needs -->
    <!-- (more info: http://www.netbeans.org/kb/41/freeform-config.html#runsingle) -->
    <target name="run-selected-file-in-src">
        <fail unless="run.class">Must set property 'run.class'</fail>
        <java classname="${run.class}" failonerror="true" fork="true">
            <classpath>
                <pathelement path="build"/>
                <pathelement location="."/>
            </classpath>
        </java>
    </target>

So i just change on <project basedir=".." to <project basediir="../build" where i want netbeans to compile. With this change netbeans will compile that file in "build" folder. AND... WOWWW... That file run correctly :D

0

精彩评论

暂无评论...
验证码 换一张
取 消