Static executables
Per Bothner
per@bothner.com
Mon Jan 30 21:20:00 GMT 2006
Andrzej Bialecki wrote:
> Tom Tromey wrote:
>>>>>>>> "Andrzej" == Andrzej Bialecki <ab@getopt.org> writes:
>>>>>>>>>>>>> Andrzej> The question is: is it possible to build static binaries on
>> Linux/BSD,
>> Andrzej> and if yes, what is the right incantation to do this, for
>> input files
>> Andrzej> consisting of a mix of *.java, *.jar, and JNI *.o objects?
>> Eg. if I'm
>> Andrzej> using Fedora Core 4, which sports gcc 4.0.2, whenever I try
>> to pass
>> Andrzej> the -static flag, the compiler complains that static
>> compilation is
>> Andrzej> not supported.
>>>> Funny, I didn't realize that this error was in there.
>> I don't remember this decision.
>>>> Andrzej> * it's not supported, because it's broken in some
>> well-defined cases
>>>> Any code that uses reflection will not work properly. This is a
>> problem because parts of the core class library depend on reflection.
>> For instance, character set translators and locales are loaded this
>> way.
That may be somewhat misleading. It's not reflection per se that is
the problem, but dynamicaly loading classes. I.e. anytime you load
a class using Class.forName may fail unless you're sure the class
is in the static executable. The static linker can't tell which
classed might be loaded using Class.forName.
> Ugh, that's pretty serious ... Ok, then this means that it's not
> possible to redistribute binaries to machines where there is no runtime
> installation. :-( Or at least not as a single file.
Er, no, it's possible. Just tricky. And more-or-less unsupported.
> If I were to have the libgcj.a (I don't think it's installed by
> default), could I pick by hand some of the .o files from it and
> explictly tell the linker to keep all symbols from those files? Would it
> work then? Or keep all symbols from the whole libgcj (and tell linker to
> ignore duplicates), would this work?
Yes, some variation of that can be made to work.
A simple kludge: In your main file do something like:
static void dummy () {
Class myclass1 = mypackage.MyClass1.class;
... etc ...
}
or:
static final Class myclass1 = mypackage.MyClass1.class;
I don't guarantee that this will actually work - but it is something
that probably *should* work, and shouldn't be very difficult to make
work if it doesn't.
--
--Per Bothner
per@bothner.com http://per.bothner.com/
More information about the Java
mailing list