在没有 gcj 的情况下组合 Java 和 C——将 C 迁移到 Java 还是将 Java 迁移到 C?
首先,我没有这样做的经验。但就像任何好的程序的开始一样,我有需要解决的问题,所以我愿意学习。
你们中的许多人可能已经熟悉 pdftk,这是一个用于处理各种 pdf 相关任务的便捷实用程序。据我所知,这些功能中的大多数都可以在更新、更轻的库/扩展中使用,除了我需要的功能(这可能是它仍然存在的唯一原因):将表单数据文件(fdf 和 xfdf)与表单合并PDF 并获取新文件作为输出。
问题是我的服务器没有 gcj,这是构建/编译 pdftk 的基础。我不知道是因为我使用的是 Solaris,还是因为其他系统管理员级别的原因,但我不会很快获得 gcj。据我所知,没有针对 Solaris 的预编译二进制文件。
所以我想可以重写MAKE文件和C代码以通过javac直接导入Java库(itext的非常古老的版本)。
但我不确定从哪里开始。我所知道的是:
- 完成后我想要一个二进制文件,这样每次使用时就不需要 Java VM。
- 当前应用程序使用 GCJ。
所以我的第一个想法是“哦,这很简单,我可能可以使用其他一些基于 C 的方法来调用这些类”,但是我没有找到一个简单的方法来执行此操作,而是找到了大量关于各个角度的冗长帖子 。
然后我在 Sun 的网站上找到了一个关于如何在 Java 类中调用其他语言(如 C)的页面 但这种方法的问题是:
- 我必须为包装器编写一个包装器
- 我可能最好跳过该部分并用 Java 编写整个内容
- 我还没有准备好,如果我可以导入已经存在的类
- 我不清楚我是否可以编译并在最后获得二进制文件,或者我是否陷入每次都需要Java的困境。
再次为我的无知道歉。我只需要一些建议和示例,说明如何用直接与 Java 一起工作的代码来替换 GCJ 相关的 C 代码。
当然,如果我问“如果我们能做到这一点,我们就已经富有了”之类的问题之一,请告诉我。
First, I have no experience doing this. But like the beginning of any good program, I have problem that I need to fix, so I'm willing to learn.
So many of you are probably already familiar with pdftk, the handy utility for handling various pdf-related tasks. So far as I can tell, most of these features are available in much newer, lighter libraries/extensions, except the one I need (and probably the only reason it still exists): merging form data files (fdf and xfdf) with a form PDF and getting a new file as the output.
The problem is that my server doesn't have gcj, which is fundamental to build/compile pdftk. I don't know if it's because I'm on Solaris or if it's for some other sysadmin-level reason, but I'm not getting gcj anytime soon. And there are no pre-compiled binaries for Solaris as far as I can find.
So I'm thinking that the MAKE file and C code can be rewritten to import the Java library (very ancient version of itext) directly, via javac.
But I'm not sure where to really start. All I know is:
- I want a binary when I'm done, so that there won't be a need for a Java VM on every use.
- The current app uses GCJ.
So my first thought was "Oh this is easy, I can probably just call the classes with some other C-based method", but instead of finding a simple method for doing this, I'm finding tons of lengthy posts on the various angles that this can be approached, etc.
Then I found a page on Sun's site on how to call other languages (like C) in a Java class. But the problems with that approach are:
- I'd have to write a wrapper for the wrapper
- I'd probably be better off skipping that part and writing the whole thing in Java
- I ain't ready for that just yet if I can just import the classes with what is already there
- I'm not clear on if I can compile and get a binary at the end or if I'm trapped in Java being needed every time.
Again, I apologize for my ignorance. I just need some advice and examples of how one would replace GCJ dependent C code with something that works directly with Java.
And of course if I'm asking one of those "if we could do that, we'd be rich already" type questions, let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您到底在寻找什么,所以我提供了几个答案。
如果您有需要运行的 java 代码,您必须:
顺便说一句,您可以在主文件夹中编译 gcj 的副本并使用它。我相信神奇的开关是
--enable-languages=java,c
(请参阅:此处 了解更多)如果您想从 java 调用 c 代码,您有四个选择:
其他平台相关选项
I'm not sure what you are looking for exactly, so I provided several answers.
If you have java code that needs to run, you must:
Incidentally, you could compile a copy of gcj in your home folder and use that. I believe the magic switch is
--enable-languages=java,c
(see: here for more)If you have c-code you want to call from java, you have four options:
Additional platform dependent options
我不确定我是否明白您在寻找什么。
如果您希望将 C 代码合并到 Java 中以生成没有 gcj 的本机二进制文件,我认为您不走运。您可以在 Java 中包含 C,但这将是一个主要的 Java 程序,这意味着您每次运行都需要 JVM。有什么事情阻止你自己编译 gcj 吗?
I am not sure if I understand what you are looking for.
If you are looking to incorporate the C code into Java to make a native binary without the gcj, I think you are out of luck. You can include the C in Java, but it would be a primarily Java program meaning you would need the JVM on each run. Is there anything stopping you from compiling the gcj yourself?