Static executables

Andrzej Bialecki ab@getopt.org
Tue Jan 31 00:02:00 GMT 2006


Per Bothner wrote:
> 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.

Ah, that's much better news. I happen to know which classes I'm going to 
load dynamically in my code, and also in all libraries that I use (I 
hope I know this ..).

>
>> 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.

Do I need to compile the libgcj.a from source, or is it distributed as a 
package (rpm?). If from source, are there any particular options to 
./configure I should be aware of?

>
> 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.

Yep, I get it now, I just need to reference the class explicitly in my 
code, otherwise linker will skip it. Makes sense.

Thank you for your help!

-- 
Best regards,
Andrzej Bialecki     <><
 ___. ___ ___ ___ _ _   __________________________________
[__ || __|__/|__||\/|  Information Retrieval, Semantic Web
___|||__||  \|  ||  |  Embedded Unix, System Integration
http://www.sigram.com  Contact: info at sigram dot com




More information about the Java mailing list