Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing libraries for a Linux executable

I have written an application in Java and succesfully compiled it using gcj. It worked surprisingly well, but I've run into a hurdle: I can only run the executable through a shell script, because I have to specify the library paths.

The libraries I need are SWT, Xerces and GNU-crypto.

Is there a way to statically link the libraries when compiling in gcj, or is this not a good idea? Alternatively, can I specify the (relative) library path while compiling?

Presently, my shell script looks like this:

#!/bin/sh
export LD_LIBRARY_PATH=./libs/:$LD_LIBRARY_PATH
exec ./MyJavaApp $*
like image 367
Paul Lammertsma Avatar asked Jun 17 '26 13:06

Paul Lammertsma


1 Answers

The idea is to make the static field "sys_paths" null so that it would construct the paths from the changed value. See the post here (Post#223 by AjaySingh516) http://forums.sun.com/thread.jspa?messageID=3744346#3744346

Class clazz = ClassLoader.class;
Field field = clazz.getDeclaredField("sys_paths");
boolean accessible = field.isAccessible();
if (!accessible)
    field.setAccessible(true);
Object original = field.get(clazz);
// Reset it to null so that whenever "System.loadLibrary" is called, it
// will be reconstructed with the changed value.
field.set(clazz, null);
try {
    // Change the value and load the library.
    System.setProperty("java.library.path", "./libs/");
    System.loadLibrary("mylibapr");
} finally {
    // Revert back the changes.
    field.set(clazz, original);
    field.setAccessible(accessible);
}

.

gcj System Properties (See: Standard properties supported by libgcj)

http://gcc.gnu.org/onlinedocs/gcj/System-properties.html

.

Solution#2 : Set System environment variable at compile time

http://linux.die.net/man/1/gcj

For this you have to use parameter -Djava.library.path=./libs/ with gcj

From gcj manual (above link):

--main= CLASSNAME

This option is used when linking to specify the name of the class whose "main" method should be invoked when the resulting executable is run.

-Dname[=value]

This option can only be used with "--main". It defines a system property named name with value value. If value is not specified then it defaults to the empty string. These system properties are initialized at the program's startup and can be retrieved at runtime using the "java.lang.System.getProperty" method.

I have never worked with gcj but as per docs these system properties can be retrieved at runtime, hence it will be portable to other systems as well.

Also see: http://gcc.gnu.org/wiki/Statically_linking_libgcj?action=show&redirect=Statically+linking+libgcj

like image 102
Gladwin Burboz Avatar answered Jun 19 '26 02:06

Gladwin Burboz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!