0

It's very interesting to use Class.forName() to load a class and get errors... I write one line code and get an error as above.

class UnsatisfiedLinkErrorTest {
    public static void main(String[] args) throws ClassNotFoundException {
        System.out.println(Class.forName("java.net.SocketOutputStream"));
    }
}

When I use java UnsatisfiedLinkErrorTest, I get an error:

> java UnsatisfiedLinkErrorTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: java.net.SocketOutputStream.init()V
    at java.net.SocketOutputStream.init(Native Method)
    at java.net.SocketOutputStream.<clinit>(SocketOutputStream.java:44)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:259)
    at UnsatisfiedLinkErrorTest.main(UnsatisfiedLinkErrorTest.java:3)

But when I run it in Idea, I get:

> java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49247:/Applications/IntelliJ IDEA.app/Contents/bin" UnsatisfiedLinkErrorTest
class java.net.SocketOutputStream

I don't know why in Idea I can get the true answer, (maybe jni?) I hope someone can tell me the reason. Thanks.

wind2412
  • 1,011
  • 1
  • 9
  • 24

1 Answers1

0

This question has been solved. I then cancelled the initialization of the class, then the error goes. The error is about the initialization, but not about finding the native library. So:

class UnsatisfiedLinkErrorTest {
    public static void main(String[] args) throws ClassNotFoundException {
        System.out.println(Class.forName("java.net.SocketOutputStream", false, null));  // will be okay!
    }
}
wind2412
  • 1,011
  • 1
  • 9
  • 24