tags:

views:

304

answers:

2

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:

  1. I want a binary when I'm done, so that there won't be a need for a Java VM on every use.
  2. 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:

  1. I'd have to write a wrapper for the wrapper
  2. I'd probably be better off skipping that part and writing the whole thing in Java
  3. I ain't ready for that just yet if I can just import the classes with what is already there
  4. 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.

A: 

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?

Luke Cycon
+1  A: 

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:

  1. Run it in a jvm. You can start that vm within your own custom c-code, but it is still using a jvm
  2. Rewrite it in another language.
  3. Compile with an ahead-of-time compiler (eg gcj)

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:

  1. Java Native Interface (JNI). It seems you found this
  2. Java Native Access (JNA). This is slower than JNI, but requires less coding and no wrapper c-code. It does require a jar and a library
  3. Create a CLI utility and use Runtime.Exec(...) to call it.
  4. Use some sort of Inter Process Communication to have the Java code ask the c-code to perform the operation and return the result.

Additional platform dependent options

  1. Use JACOB (win32 only: com access)
KitsuneYMG
JACOB is another option for calling C code from Java
finnw
If I can get gcj compiled to my home dir, I have such a huge rant. So far I think I'm missing something. It says "no input files" when I put in `gcc --bindir=~/bin --enable-languages=java,c`, does this mean I need to have a local version of gcc as well that I build? Because attempts to do gcj solo previously seemed undoable without compiling gcc with gcc. More links are welcome.
Anthony
@finnw Thanks. I have never heard of JACOB. Adding to answer
KitsuneYMG
Sorry to nub it up, but just a quick confirm: I do need to have the full gcc tar before I can compile gcj to my home dir?
Anthony
Anthony: Yes - gcj is part of gcc.
caf
I know that. The server already has gcc. But I can't figure out how to compile gcj locally without building all of gcc (which is huge and I'm not sure if it will be redundant).
Anthony
you don't run `gcc --bindir=~/bin --enable-languages=java,c` you run `./configure --bindir=~/bin --enable-languages=java,c` and follow it with `make`
KitsuneYMG