Blackberry 5.0 - 添加对 Java 库项目的引用
在我看来,我想要实现的目标非常简单,但却变得非常痛苦。
我有 ProjectA,它是一个 BlackBerry 应用程序项目。我有 ProjectB,它是一个 Java 库项目。我想从ProjectA引用ProjectB。我可以添加引用,但是当我运行 ProjectA 时,它不起作用。我有这两个项目的源代码,并且都是使用 Java 编译器 1.4 编译的。
我尝试了多种方法,但由于某种原因一切都失败了:
1. ProjectB 上的 pre-verify.exe 它失败并显示错误“JAR 文件创建失败,错误 -1” 我可以看到 cod 和 jar 文件已创建,但是当将 jar 文件添加到 ProjectA 并运行它时,它不起作用。不确定是否需要添加 .cod 文件。
2.创建新的 BlackBerry Library Project 并在 ProjectA 中引用它 我创建一个新项目 ProjectC,然后将 ProjectB 的 jar 添加到其中。然后我在 ProjectA 中添加对 ProjectC 的引用。但我无法从 ProjectB 导入类,
请提出解决办法。 我正在使用 Eclipse 插件并依赖 Eclipse 的构建功能
It seems to me that what I'm trying to achieve is incredibly simple, yet is becoming incredibly painful.
I have ProjectA which is a BlackBerry Application project. I have ProjectB which is a Java library project. I want to refer to ProjectB from ProjectA. I can add a reference but when I run ProjectA, it doesn't work. I have source code for both the projects and both are compiled using Java compiler 1.4
I have tried multiple things but everything fails for some reason:
1. pre-verify.exe on ProjectB
It fails with an error "JAR file creation failed with error -1" I can see that the cod and jar files have been created but when add the jar file to ProjectA and run it, it doesn't work. Not sure if I need to add the .cod file.
2. Create new BlackBerry Library Project and reference it in ProjectA
I create a new project ProjectC and then add the jar of ProjectB to it. Then I add a reference to ProjectC in ProjectA. But I cant import classes from ProjectB
Pls suggest a way out.
I'm using Eclipse Plug-in and relying on Eclipse's build capabilities
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己想出了答案。在此发布以防有人偶然发现这一点。步骤如下:
在 JAR 上运行 preverify.exe
preverify.exe -verbose -classpath "C:/Program Files/Research In Motion/BlackBerry JDE 5.0.0/lib/net_rim_api.jar" jarname.jar
如果您是幸运的是,您不会遇到任何问题,然后就完成了。但我不够幸运。我收到以下错误
有两个可能的原因:
安装目录)到 PATH
jar.exe 上的 cvfm 或 -cfm 选项无法执行。我不知道原因,但解决此问题的方法是使用 -cf 选项,指向 .class 文件但不使用清单文件。这是一个
示例:
"C:\Program Files\Java\jdk1.6.0_26\bin\jar.exe" -cf "output\json-1.0.jar" tmp12996/
tmp12996 包含预先验证的 .class 文件。
除了我上面列出的问题之外,您可能还会遇到其他问题。
从上述步骤创建 jar 后,请确保其结构符合您的预期。一种检查方法是将 .jar 重命名为 .zip,将其解压缩,然后检查。如果它不符合您的需要,您可以更改结构,然后重新打包它(不过我不会做任何重大更改)
然后将这个新建的 jar 添加到您的 BlackBerry 应用程序作为参考,即添加到 Java 构建路径 。
就是这样!你可以走了!运行您的应用程序!
当您尝试在模拟器中运行时,您可能会遇到错误,指示模块包含验证错误。导致此问题的一个可能原因是您的库(原始 JAR)包含与 J2ME 或 BB JRE 不兼容的 API。当您独立构建库时,您可能不会收到编译器错误,因为它是针对 Java 1.4(或无论您的版本是什么)编译的。解决问题的最佳方法是将所有代码移至 BB App 项目中,然后构建它。这会预先告诉您所有问题。您根据需要进行更改,然后将代码移回库。如果您没有正在使用的库(例如第 3 方库)的源代码,那么您可能会运气不好!还要记住,除了我偶然发现并解决的问题之外,可能还有其他问题。
我详细记录了这一点,因为我花了很多时间才弄清楚这一切;至少可以说,是最令人沮丧的!
Figured out answer myself. Publishing here in case someone stumbles upon this. Here are the steps:
Run preverify.exe on the JAR
preverify.exe -verbose -classpath "C:/Program Files/Research In Motion/BlackBerry JDE 5.0.0/lib/net_rim_api.jar" jarname.jar
If you are lucky, you won't run into any issues and you will be done. But I wasn't lucky enough. I got the below error
There are two possible causes of this:
installation directory) to PATH
cvfm or -cfm option on jar.exe fails to execute. I'm not aware of the reason but the way to fix this is to use -cf option, point to the .class files but don't use the manifest file. here is an
example:
"C:\Program Files\Java\jdk1.6.0_26\bin\jar.exe" -cf "output\json-1.0.jar" tmp12996/
tmp12996 contains the preverified .class files.
You may run into different issues other than the one I've listed above.
Once jar is created from above step, make sure that it's structure is as you anticipate. One way to check is to rename the .jar to .zip, unzip it and then check it. If it is not as you need, you can change the structure and then repack it (I wouldn't do any major changes though)
Then add this newly built jar to your BlackBerry application as a reference i.e. add to Java Build Path in your eclipse and Check it in Order and Export window.
That's it! You are good to go! Run you app!
You may face error indicating that the module contains verification errors when you try to run in the simulator. One possible cause of this issue is that your library (the original JAR) contains APIs that are not compatible with J2ME or BB JRE. You may not get a compiler error when you build your library independently as it is compiled against Java 1.4 (or whatever your version is). Best to figure the issue out is to move all your code into your BB App project and then build it. That will tell you all the issues upfront. You make the changes as required and then move the code back to the library. If you don't have source code for the library you are using (like a 3rd party library), you may be out of luck! Also remember that there could be other issues than what I've hit upon and solved.
I'm documenting this at length as it has taken an awful amount of time for me to figure all this out; and to say the least, was most frustrating!
我找到了另一个解决方案。如果在预验证 JAR 文件时出现错误 -1,只需运行一次库应用程序即可。因为如果您不运行该应用程序,可交付成果文件夹将为空。确保该文件夹不为空。
I found another solution. If you get error -1 while preverifying your JAR file, just run your library application once. Because if you don't run the application, the deliverables folder will be empty. Make sure this folder is not empty.