Automatisches Ausfuehren einer Funktion beim Laden eines Jar files

Clemens1509

Neues Mitglied
Hallo,

ih moechte ein Jar file mit mehreren Klassen schreiben. Beim ersten Ausfuehren irgendeiner Funktion dieses JAR files sollte zuvor eine Initialisierungsfunktion ausgefuehrt werden.

Ich habe schon mal geschaut: Innerhalb einer Klasse ist das durch statische member moeglich, die fuer die Initialisierung einer Methode aufrufen.

Ist dies auch fuer mehrere Klassen ohne gemeinsame Basisklasse moeglich ?

Im aktuellen Beispiel ist das JAR file ein JNI Interface mit mehreren Klassen und es sollte automatisch ein shared object bzw DLL geladen werden ohne, dass der aufrufer dies explizit ausfuehrt.

sys.loadlibrary( "my_jni_dll.so") oder so aehnlich ...

Ist dies moeglich ?

Gruss
Clemens
 
Willst du sowas vielleicht in einen Static block packen?

Java:
static{
 // anweisung
}
 
hier mal der source des 0.4-oberzalek release der JShortCut lib

Java:
static {
	// The CLASSPATH searching code below was written by Jim McBeath
	// and contributed to the jRegistryKey project,
	// after which it was modified and used here.
	try {
	    String appDir = System.getProperty("JSHORTCUT_HOME");
	    	// allow application to specify our JNI location
	    if (appDir!=null) {
	        // application told us to look in $JSHORTCUT_HOME for our dll
		File f = new File(appDir,"jshortcut.dll");
		String path = f.getAbsolutePath();
		System.load(path);	// load JNI code
	    } else {
	        // No specified location for DLL, look through PATH
	        System.loadLibrary("jshortcut");
	    }
	} catch (UnsatisfiedLinkError ex) {
	    // didn't find it in our PATH, look for it in CLASSPATH
	    String cp = System.getProperty("java.class.path");
	    boolean foundIt = false;
	    while (cp.length() > 0) {
		int x = cp.indexOf(File.pathSeparator);
		String dir;
		if (x >= 0) {
		    dir = cp.substring(0,x);
		    cp = cp.substring(x+1);
		} else {
		    dir = cp;
		    cp = "";
		}
		if (dir.length()>4 &&
		    dir.substring(dir.length()-4).toLowerCase().equals(".jar")){
		    // If the classpath contains a jar file,
		    // then we look in the directory
		    // containing the jar file.
		    x = dir.lastIndexOf(File.separator);
		    if (x>0)
			dir = dir.substring(0,x);
		    else
			dir = ".";
		}
		File f = new File(dir,"jshortcut.dll");
		if (f.exists()) {
		    String path = f.getAbsolutePath();
		    System.load(path);	// load JNI code
		    foundIt = true;
		    break;
		}
	     }


        if (!foundIt) {
            try {
            ClassLoader cl = JShellLink.class.getClassLoader();

            String libname = "jshortcut_" + System.getProperty("os.arch") + ".dll";

            InputStream in = cl.getResourceAsStream(libname);
            if (in == null)
                throw new Exception("libname: jshortcut.dll not found");
            File tmplib = File.createTempFile("jshortcut-",".dll");
            tmplib.deleteOnExit();
            OutputStream out = new FileOutputStream(tmplib);
            byte[] buf = new byte[1024];
            for (int len; (len = in.read(buf)) != -1;)
                out.write(buf, 0, len);
            in.close();
            out.close();

            System.load(tmplib.getAbsolutePath());

            foundIt = Boolean.TRUE;

            System.out.println("jshortcut.dll loaded via tmp generated pathname: " + tmplib.getAbsolutePath());
            
            } catch (Exception e) {
              foundIt = Boolean.FALSE;
            }
        }

	     if (!foundIt) {
		 // we did not find it in CLASSPATH
             System.out.println("failed loading jshortcut.dll");

		 throw ex;	// throw the can't-find-library exception
	     }
	}
    }

so bald also die klasse geladen wird wird automatisch versucht die DLL zu laden ... falls das nicht geht wird diese aus dem JAR in TEMP entpackt und dann geladen ...

vielleicht ist es sowas was du suchst ...

aber um das für ein gesamtes JAR zu machen müsstest du sowas entweder in JEDE klasse schreiben oder dich darauf beschränken über factories oder controller zu arbeiten die dann diesen static enthalten ...
aber einfach nach dem motto : egal welche klasse aus der lib ... aber so bald das JAR geladen wird ... NEIN ... das geht NICHT ...
 

Zurück
Oben