0

I've two classes:

public class UThreadApp {
    public static void main(String[] args) {
        newUThread("umain", args);
            ...
    }
    native void newUThread(String method, String[] args);
}

public class App extends UThreadApp {
    public static void umain(String[] args) {
        ...
    }
}

Application is executed as java App.

App inherits main from UThreadApp such that main calls App.umain. I've to get programmatically the main class name App from Java or JNI so as to call App.umain from JNI code. Do you see a way to do that?

Idioms such that new Object(){}.getClass().getEnclosingClass() don't work since they return UThreadApp.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
francesc
  • 343
  • 3
  • 12

1 Answers1

1

Static methods have no class, and no this reference. Your App class will “inherit” main insofar as that main is usually callable as App.main as well. But you cannot override static methods in the common sense. Once the method got invoked, there is no way to determine what name was used to invoke it. So nothing you do, on the java side or in native code, will give you the information you desire. Apart from hacking the java executable and figuring out its command line arguments or similar brutal and unsupported approaches, that is.

Note: You also have an error where the static main tries to call the non-static newUThread.

What you could do is something like this, to leave the static scope and get a reasonable this pointer:

public class UThreadApp {
    protected void UThreadApp(String[] args) {
        newUThread("umain", args);
            ...
    }
    native void newUThread(String method, String[] args);
}

public class App extends UThreadApp {
    public void main(String[] args) {
        new App(args);
    }
    public App(String[] args) {
        super(args);
    }
}
Community
  • 1
  • 1
MvG
  • 57,380
  • 22
  • 148
  • 276
  • I see... but I was trying to separate system's code (UThreadApp) from application's code (App) as much as possible. (Note: of course newUThread shoud be static). Perhaps there is a JNI function which can extract the first argument (java App) – francesc Jul 21 '12 at 07:31
  • On Unixoid systems you can have a look at `/pfoc/self/cmdline`. There might be similar solutions for other platforms. Or you might change you call requirements to always invoke `UThreadApp.main`, perhaps via the `Main-Class` of a jar filoe, and pass the name of the application to that. – MvG Jul 21 '12 at 08:56