Loading a Java class with native code dependency

I am writing a program that loads all classes from a specified folder.

Some classes create objects in a static block, and these objects, in turn, create some of their own calls during their creation. Since I don't have a native library, I see java.lang.UnsatisfiedLinkError

Here is an example script.

Class A extends SuperA
{
  public static B b = new B(); 
  ...
  ...
}

Class B
{
   public B()
   {
      someNativeCall(); // Causing the LinkError
   }
}

Class.forName("A"); // Loading of Class A

I am loading class A here to check out some of its properties, such as its superclasses, etc. Therefore, it doesnโ€™t even matter to me whether B is created or not :) Since there can be many classes in this folder, such as A, is there a general way to make sure that loading classes like A is not interrupted?

Update: I know that a native library should be present and how to add it. But I do not have a native library, and therefore I ask about it.

+3
5

.DLL .so, , . VM loadlibrary .dll/.so, , UnsatisfiedLinkError.

UnsatisfiedLinkError, , , .

, , Java Dissasembeler, javap. , - javap sun.tools.javap, - $JDK\lib\tools.jar

+3

Class.forName, , ( , ):

class X {

    static { if (true) throw new ExceptionInInitializerError("OH NOES!!!"); }

}

public class Y {
    public static void main(String[] args) throws Throwable{
        // Breaks:
        System.out.println(Class.forName("X").getSuperclass());
        // Works:
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        System.out.println(Class.forName("X", false, cl).getSuperclass());
    }
}
+2

A, B, B, B.

, ClassLoader B, , - . A ClassLoader , .

+1

, , ? , .

+1

Why do you have a class without a native library? A derived class will not work if the base class cannot be initialized. Use -Djava.library.path to specify the location of your own library.

0
source

Source: https://habr.com/ru/post/1726800/


All Articles