这里第一次发帖,会尽量简洁。这是一个经典的“无法访问Applet文件”的问题,但我遇到了一些特殊的困难。如何使用getCodeBase()在Java Applet中查找并加载文件?
我想这个文件改写:
到模板的小程序加载libpd在PureData的(puredata.info)制造(https://github.com/libpd/libpd)补丁程序......这个已经工作在正常在非小应用程序的Java程序(见上文),其中主功能发现使用所述贴片主要功能:
PdBase.openAudio(0, outChans, (int) sampleRate);
int patch = PdBase.openPatch("samples/com/noisepages/nettoyeur/libpd/sample/test.pd");
PdBase.computeAudio(true);
它尝试文件加载路径并进入int变量的原因是THA T中的核心函数本身做这个有:
public synchronized static int openPatch(File file) throws IOException {
if (!file.exists()) {
throw new FileNotFoundException(file.getPath());
}
String name = file.getName();
File dir = file.getParentFile();
long ptr = openFile(name, (dir != null) ? dir.getAbsolutePath() : ".");
if (ptr == 0) {
throw new IOException("unable to open patch " + file.getPath());
}
int handle = getDollarZero(ptr);
patches.put(handle, ptr);
return handle;
}
public synchronized static int openPatch(String path) throws IOException {
return openPatch(new File(path));
}
这是因为PD试图通过给一个int“把手”(dollarZero,遗留原因),以确定每个补丁,使INT手柄被传来传去打开并关闭修补程序文件。
所以现在。我试图在Applet中加载相同的文件,因为我相信它在客户端运行并且不知道我在说什么路径,所以我在java.net.URL上阅读并尝试构建的变化:
patchURL = new URL("test.pd");
PdBase.openPatch(patchURL.getPath().toString());
和
URL url = this.getClass().getResource("test.pd");
通过previous questions在init启发()和start()小应用程序的功能,把原始主成局部静态方法的声音()。
我得到的只是空指针。我会想我需要的只是一个简单的getDocumentBase(),但似乎无法使它工作。任何人?
为了跟进,做'String pd = this.getCodeBase()。toString();'然后'pdPath = pd +“../ samples/com/noisepages/nettoyeur/libpd/sample/test.pd” ;'把我带到了正确的路径(当然这不是完全的代码路径)......但是在这一点上我得到'文件未找到',这与'System.out.println(pdPath) ;'给我的文件的确切路径,我可以看到它! – pland 2012-02-04 15:16:10
是否有'openPatch(URL)'或'openPatch(InputStream)'方法?如果不是,该API将无法使用小程序。 – 2012-02-04 15:17:03
不,并且不够公平。如果我沟通小程序(我试图给一群音频学生提供使用libpd和Java的最简单可能的途径),可以通过面板来做到这一点吗? – pland 2012-02-04 15:20:04