1

I have java web application which is deployed to Tomcat. Application calls low level C library via JNI. Everything works fine when I deploy my application at first time, but as soon as I trying to redeploy - application crashes with following error: java.lang.UnsatisfiedLinkError: Native Library /opt/apache-tomcat/apache-tomcat-7.0.57/shared/lib/libGenderizer.so already loaded in another classloader

  1. I've created and put my .so object under /opt/apache-tomcat/apache-tomcat-7.0.57/shared/lib.

  2. In catalina.sh (file which starts tomcat) I've added following line which sets library path for tomcat. JAVA_OPTS="$JAVA_OPTS -Djava.library.path=/opt/apache-tomcat/current/shared/lib"

  3. I am redeploying my application using maven mvn tomcat7:redeploy

Could you please help me to resolve current issue? I am doing System.gc() but unfortunately it doesn't help.

   public class GenderService {

            private static Logger logger = Logger.getLogger(GenderService.class);

            static {
                try {
                    System.gc();
                    System.loadLibrary("Genderizer");
                }
                catch(UnsatisfiedLinkError e){
                    logger.error("Native code library failed to load.\n", e);
                }
            }

            private native String genderize(String name);


            public Gender identifyGender(Customer customer){
                Gender gender = determineGender(customer);
                return gender;
            }
    }
Wild Goat
  • 3,509
  • 12
  • 46
  • 87
  • 1
    possible duplicate of [.dll already loaded in another classloader?](http://stackoverflow.com/questions/1030792/dll-already-loaded-in-another-classloader) – Olaf Kock Feb 11 '15 at 20:48

1 Answers1

0

Already asked on SO. See Apache Tomcat Wiki for a helpful answer.

Community
  • 1
  • 1
  • I've read it but unfortunately didn't get how does it works. If I wrap my library into JAR how do I invoke it? – Wild Goat Feb 11 '15 at 13:11
  • If you add the JAR to shared/lib all the classes in the JAR are available to your application as if they were located in WEB-INF/classes or WEB-INF/lib. So you simply use ist as before. – Andreas Vogler Feb 11 '15 at 13:26
  • before I was using them directly in java since I could `import` it on top of the class. Since file is not in my project, how do I invoke its method? Lets say I have `AClass` which as method `AMethod(String)`, could you please give me example how do I invoke it if jar is inside /shared/lib? Thanks! – Wild Goat Feb 11 '15 at 14:01
  • I don't understand: why is it not in your project? Your GenderService is still supposed to be part of your project. You just deploy it in separate JARs. – Andreas Vogler Feb 11 '15 at 14:06
  • Hm.. Right now I am developing and resolving all dependencies via maven. You are saying that I have to generate Wrapper jar and add it as external jar from /shared/lib? Also in catalina.properties I have to specify folder for jars? `shared.loader=${catalina.base}/shared/lib/*.jar` – Wild Goat Feb 11 '15 at 14:10
  • Yes. For building with maven I guess you could build it as a separate artifact and add this as a compile-time dependency to your main project. – Andreas Vogler Feb 11 '15 at 14:25
  • Hmm., No Idea, doesn't work. This is how I am trying to import separate .jar but no luck unable to resolve dependencies on compile time. ` com.thehutgroup.GenderizerWrapper GenderizerWrapper system ${basedir}/src/main/Genderizer/GenderizerWrapper.jar 1.0 ` – Wild Goat Feb 11 '15 at 15:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70719/discussion-between-andreas-vogler-and-wild-goat). – Andreas Vogler Feb 11 '15 at 15:34
  • hi i tried this solution from the tomcat wiki, but when undeploying my app i get an error, that he cannot delte the jar files in the WEB-INF/lib folder – wutzebaer Jan 22 '16 at 08:45