如何使用 getCodeBase() 在 Java Applet 中查找并加载文件?
第一次在这里发帖,尽量简洁。这是一个典型的“无法访问 Applet 中的文件”问题,但我遇到了特别的困难。
我正在尝试重写此文件:
到模板小程序中以加载 libpd (https://github.com/libpd/libpd) 在 PureData (puredata.info) 中制作的补丁...这已经可以在非 applet Java 程序中的正常 Main 函数中使用(见上文),其中 main 函数使用以下命令查找补丁:
PdBase.openAudio(0, outChans, (int) sampleRate);
int patch = PdBase.openPatch("samples/com/noisepages/nettoyeur/libpd/sample/test.pd");
PdBase.computeAudio(true);
它尝试将路径和文件加载到 int 变量中的原因是核心函数本身通过以下方式执行此操作:
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");
受到 init() 和 start( 中的之前的问题的启发) 函数小程序的,将原来的main变成本地静态方法sound()。
我得到的只是空指针。我本以为我需要的只是一个简单的 getDocumentBase(),但似乎无法使其工作。有人吗?
First time posting here, will try to be succinct. This is a classic 'can't access file within an Applet' problem, but I'm having a particular difficulty with it.
I'm trying to rewrite this file:
into a template applet to load libpd (https://github.com/libpd/libpd) patches made in PureData (puredata.info)...this already works in a normal Main function in a non-applet Java program (see above), where the main function finds the patch using:
PdBase.openAudio(0, outChans, (int) sampleRate);
int patch = PdBase.openPatch("samples/com/noisepages/nettoyeur/libpd/sample/test.pd");
PdBase.computeAudio(true);
The reason it tries to load the path and file into a int variable is that the core function itself does this with:
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));
}
This is because PD tries to identify each patch by giving an int 'handle' (dollarZero, for legacy reasons), so that int handle gets passed around to open and close the patch file.
So now. I'm trying to load the same file in an Applet, so since I believe it runs 'in the client' and won't know what path I'm talking about, I read up on java.net.URL and tried to build variations of:
patchURL = new URL("test.pd");
PdBase.openPatch(patchURL.getPath().toString());
and
URL url = this.getClass().getResource("test.pd");
inspired by previous questionsin the init() and start() functions of the applet, turning the original main into a local static method sound().
All I get is null pointers. I would've thought all I needed was a simple getDocumentBase(), but can't seem to make it work. Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
libpd 只是 Pure Data 之上的一个薄包装器,Pure Data 不了解 Java 中的 URL 或输入流。 openPatch方法只是将补丁名称和目录发送给Pd,然后Pd会尝试打开相应的文件。因此,小程序已经过时了,除非您愿意修改安全策略。
关于查找文件,简单的示例程序是 libpd Eclipse 项目的一部分。它应该在 Eclipse 中运行,并且补丁的硬编码路径是相对于 Eclipse 中的项目根目录的。如果您希望代码在不同的设置中运行,则必须相应地调整路径。
libpd is just a thin wrapper on top of Pure Data, and Pure Data doesn't know about URLs or input streams in Java. The openPatch method merely sends the patch name and directory to Pd, and then Pd will try to open the corresponding file. So, applets are out, unless you're willing to tinker with security policies.
About finding files, the simple sample program is part of the libpd Eclipse project. It's meant to be run in Eclipse, and the hard-coded path to the patch is relative to the project root in Eclipse. If you want your code to run in a different setting, you have to adjust your paths accordingly.