How to Provide Native Methods in an Executable?

Hey there,
I'm writing a native Java launcher as a replacement for java.exe (or eclipse.exe). So far I'm only on Windows NT x64. Things work pretty well: My native executable launches the JVM using the Invocation API and calls a static `main' method.
Now I thought it was a good idea if my executable could provide the implementations of a few native methods so the `jvm.dll' can communicate with the executable that it was loaded by. This would free me from having to locate and load a shared library at runtime. I expected this to "just work," but alas, it didn't. Providing a simple native method such as
JNIEXPORT jboolean JNICALL
Java_com_phrood_kourou_Kourou_available0(JNIEnv* env, jclass cls) {
return JNI_TRUE;
in the executable and calling it from Java through JNI, I get an `UnsatisfiedLinkError'. Why is that so? Both the executable and the DLL live in the same process, so shouldn't the C function be found without further concern?
Then I came up with the somewhat experimental idea and just loaded the EXE file from Java by `System.load(pathToExe)'. I never expected this to work, but---it did!
That's good news, of course. Only I'm afraid that's a lot more memory-consuming than it needs to be. My EXE file has some 200 KB and counting, and I only need I a handful of small native-method implementations. I could write a DLL, of course, but I'd like to do without an extra file and an extra project to set up and maintain.
So, my question is: Why do I have to load the EXE into the same process a second time? Is there a more elegant solution?
Regards,
-- Phil

It doesn't work anyway. Although I can load my EXE as a DLL, the newly-loaded image has its own address space, so I cannot use that to call back the EXE.
I think I can acomplish what I need without a DLL altogether. Hopefully.
I want the usage of my executable to be as simple as using `java.exe'. Clients should just write a configuration file, rename and start or double-click the EXE and that's it. An MSI would be pointless here, since the usage of the launcher is completely generic.
More importantly, maintaining cross-platforms C++ projects with Eclipse CDT is possible but a bit of a hassle. I already have two projects: one for the EXE and one for the Java companion Jar. Creating a new DLL project would require me to maintain 3 Eclipse build configurations for Windows, Linux, and Mac; and even another three should I support the x86 architecture. I'd be happy if I could do without.
What strikes me is that you folks try to teach me what I should do or not do, even call my problem a "nonproblem," rather than just help me out and try to answer my questions. None of the answers here were helpful to me, but I think I figured it out.
Nonetheless, thanks for taking the time to read and answer.

Similar Messages

  • How to use native method

    how to use native methods

    ----- BEGIN CANNED RESPONSE -----
    Download and read Sheng Liang's book, ftp://ftp.javasoft.com/books/specs/jni.pdf
    Read the JNI specs on java.sun.com site ( http://java.sun.com/j2se/1.4.2/docs/guide/jni/index.html )
    Beware with the JNI tutorial on the java.sun.com site - it is slightly outdated, and the samples need modifications to compile
    ----- END CANNED RESPONSE -----

  • How to put native methods in jar file

    Hi.I have an executable jar file(test.jar) that makes use of other jar files and native methods. For example, test.jar uses Jnpout32.dll. . test.jar , the other jar files and the native methods are all in the same directory. When I created test.jar I wrote a manifest file which specifies the class path of the other jar files and the native methods as follows and provided it as an option in the jar cfmev command.
    Class Path: jdbc.jar Jnpout32reg.dll  //content of manifest.txt  file that I provided to the jar command
    //carriage return I then run the command java -jar test.jar to run the file.
    All I am seeing is the following error message
    Exception in thread "AWT EventQueue-o
    "Java.lang.NoClassDefFoundError.
    gnu/io/SerialPortEventListner"
    {code}
    Any ideas? thanks in advance!
    Edited by: Nostalgia on May 22, 2009 8:05 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    You seem to be confusing Java's classpath with your os's path. The classpath only applies to Java classes. See if this discussion helps:
    http://www.inonit.com/cygwin/jni/helloWorld/load.html

  • How to track how long a program method takes to execute?

    So i am writing a radix sort in java and i want to see how long it takes to run. I was wondering how i would get the exact time that a method would take?
    Calendar cal = new GregorianCalendar();
              int ms = cal.get(Calendar.MILLISECOND);
    I am not sure where to start from here or if this is even the correct way to do it, Any help would be great : ).

    You can use System.currentTimeMillis. Call it before the method starts and after the method ends. Subtract the two values and your result will be how many milliseconds it took.

  • How to invoke bean method after query executed?

    Jdeveloper 11.1.1.2:
    I have a bean that backs a JSF page.
    The bean has a method that shows/hides certain GUI components based on the values of other components.
    I would like to invoke this method automatically after the page has been fully loaded.
    I tried invoking the method in the AfterPhase of the f:view and at bean construction time. Both approaches failed.
    I think I need a listener that gets invoked after the query has been executed.
    Any idea?
    thanks

    Hi,
    if the bean is a backing bean and it is supposed to hide components, then why don't you create a reference of the components to the backing bean so that the displayed property is dynamically evaluated ? Sounds like this is what you want to do
    Frank

  • Urgent - How to call native methods from J2ME

    Hi,
    I have to do some very heavy processing work, so I have my code written in C. And now I want these native functions to be called from a J2ME application. I had thought this was not possible and J2ME did not support something like JNI as J2SE did.
    However, when contacted with Sun Technical Support we were advised to use KNI in J2ME. I went through the KNI documentation and did not find any example to do so.
    On reading about it I understood that it cannot be implemented by developers as the native functions if any using KNI should be implemented while compiling the KVM.
    I would like to know whether my understanding is correct, or is it possible to call native functions from J2ME. Say I would like to have my native functions called from J2ME and run it on my Nokia N73 or other such phones.
    Experts please clear this.
    Regards,
    Kamalakshan

    Hi,
    You are right in your assumptions regarding KNI. Here is some related thread that might also help. There has been [some work|http://developer.symbian.com/main/downloads/papers/MIDlet_Native_Services_Framework/MIDlet_Native_Services_Framework_v1.1.zip] done in accessing native services through a MIDlet.
    Mihai

  • How to interface native methods in LINUX???

    As the cc command in Solaris is :
    cc -G -I/usr/java/include -I/usr/java/include/solaris \filename.c -o libfile.so
    what will be the similiar command in LINUX system????

    Take a look at http://fir4o.hit.bg/nativetest-src.zip -
    There is a Makefile which I used to compile shared library for Linux.

  • How to view the source code for a Native Method

    hi
    i am using a Native method Math.pow() ;
    but since it is a native method it is taking more time to execute ;
    since this method i had to call at least 700 times it is affecting the performance of the application ;
    so i am thinking to write the user defined method based on the logic which has been implemented in the pow() method ;
    for that i need to know in which .c file this method has been written ;
    or else it can not be viewed at all ( if it is in the .dll)
    can u help me out
    nik

    Hi!
    Here is part of StrictMath.java code.
    * The class <code>StrictMath</code> contains methods for performing basic
    * numeric operations such as the elementary exponential, logarithm,
    * square root, and trigonometric functions.
    * <p>
    * To help ensure portability of Java programs, the definitions of
    * many of the numeric functions in this package require that they
    * produce the same results as certain published algorithms. These
    * algorithms are available from the well-known network library
    * <code>netlib</code> as the package "Freely Distributable
    * Math Library" (<code>fdlibm</code>). These algorithms, which
    * are written in the C programming language, are then to be
    * understood as executed with all floating-point operations
    * following the rules of Java floating-point arithmetic.
    * <p>
    * The network library may be found on the World Wide Web at:
    * <blockquote><pre>
    * http://metalab.unc.edu/
    * </pre></blockquote>
    * <p>
    * The Java math library is defined with respect to the version of
    * <code>fdlibm</code> dated January 4, 1995. Where
    * <code>fdlibm</code> provides more than one definition for a
    * function (such as <code>acos</code>), use the "IEEE 754 core
    * function" version (residing in a file whose name begins with
    * the letter <code>e</code>).
    * @author unascribed
    * @version 1.9, 02/02/00
    * @since 1.3

  • How to tell how many times a method has executed?

    Hi, all
    I've got a main class (Model.java) that repeatedly calls a step() method in a sub class (Ocean.java). I want Ocean.java to execute its step() method following one set of rules for the first time it is called by Model.java, and a different set of rules for all executions 2++. I was just going to use an if/else in Ocean.java's step() method to make it switch based on a boolean haveIExecutedThisStepOnce, or something like that. Is there some way to tell Ocean.java how many times it has executed the step() method? I'm using the RePast code libraries; Model.java uses a method called getTickCount(). It returns a double value of number of times Model.java's step() method has been executed, in which Model.java tells Ocean.java to execute its step() method. Should I pry that getTickCount() value out of Model.java and get it over to Ocean.java's step() method--and if so, how? Or, is there a better way to do this that I'm not aware of?

    Why do you think that this field has to be public?
    Arguably if there's some functionality that is tightly associated with this counted method, then it should be part of the same class.
    Alternatively, think about the counting variable. It should be with the thing that uses it.
    So if class A wants to invoke class B method c once, do something else, and then invoke c some more, then that counting is really more an aspect of A than of B, and in that case the state should be in A. In that case the field really counts the number of times that A has invoked B.c, not the number of times that B.c has been invoked overall. Maybe that's what you want.
    On the other hand if you really want to count the number of times that B.c has been invoked overall, then it's an aspect of B (or more likely an instance of B). In that case, rather than expose the counting state and the counted method, it would probably be better to create a new method d(). d() would invoke c(), doing something extra depending on the counting state. Make c() private. Like this:
    class B {
      private boolean c_has_been_called = false;
      public void d() {
        c();
        if (!c_has_been_called) {
          doWhateverYouNeedInThisCase();
          c_has_been_called = true;
      private void c() {
        // and c() does whatever it normally does
    }

  • How to view the source code for Native Method

    hi
    i am using some native methods in to my code ;
    can anybody tell me how to view the source code for the same ;
    nik

    Buy/acquire a C/C++/assembly code disassembler and run the shared library through it.

  • How to create new java objects in native methods?

    Hello,
    Is it possible to create java objects and return the same to the java code from a native method?
    Also is it possible to pass java objects other than String objects (for example, Vector objects) as parameters to native methods and is it possible to return such objects back to the java code?
    If so how can I access those objects (say for example, accessing Vector elements) inside the native code?
    What should I do in order to achieve them?
    Regards,
    Satish

    bschauwe is correct in that constructing Java objects and calling methods on Java objects from native code is tough and takes some study. While you're at it, you might want to check out Jace, http://jace.reyelts.com/jace. It's a free open-source toolkit that really takes the nastiness out of doing this sort of stuff. For example,/**
    * A C++ function that takes a java.util.Vector and plays around with it.
    public void useVector( java::util::Vector& vector ) {
      // Print out all the contents of the vector
      for ( Iterator it = vector.iterator(); it.hasNext(); ) {
        cout << it.next();
      // Add some new elements to the vector
      vector.addElement( "Hello" );
      vector.addElement( "world" );
    } All this code just results in calls to standard JNI functions like FindClass, NewObject, GetMethodID, NewStringUTF, CallObjectMethod, etc...
    God bless,
    -Toby Reyelts

  • How to use getContent() method in custom JSP Provider to display a HTML Pag

    Hi,
    If anybody knows how to use getContent() method to use in custom jsp providers (developed by ourselves) so that it can be used to retrieve a jsp page (a simple html page) ..
    I want the code in the provider java file to for the getContent method...
    Pls. get back to me asap....if any body has implemented a custom jsp provider...as it's urgent...
    I have alreday placed the JSP file in the directory structure /etc/opt/SUNWps/desktop/default/channel_dir..But still the jsp is not being displayed..
    Pls get me the getContent() method code to retrive the JSP file..
    satyabrata

    Hi,
    You don't have to do anything in the custom JSPProvider's getContent method except the call {  return super.getContent(request,response); } . If all you want is just to show your jsp, then create a channel from the default JSPProvider, and edit the property contentPage of that channel from samplecontent.jsp to your jsp name, save the changes and login again. You should see your JSP.
    Sanjeev.

  • How do I declare a native method outside of the main class?

    Hi
    This is a JNI particular question.
    I am having a problem with generating the header .h file after executing javah. The file is generated correctly but is empty under certain circumstances.
    This is the 'empty file:
    =================
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class gui_GuiMain */
    #ifndef Includedgui_GuiMain
    #define Includedgui_GuiMain
    #ifdef __cplusplus
    extern "C" {
    #endif
    #ifdef __cplusplus
    #endif
    #endif
    This is what it should look like:
    =========================
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class gui_GuiMain */
    #ifndef Includedgui_GuiMain
    #define Includedgui_GuiMain
    #ifdef __cplusplus
    extern "C" {
    #endif
    * Class: gui_GuiMain
    * Method: getValueOIDTestMIB
    * Signature: ()V
    JNIEXPORT void JNICALL Java_gui_GuiMain_getValueOIDTestMIB
    (JNIEnv *, jobject);
    #ifdef __cplusplus
    #endif
    #endif
    The header file becomes "empty" when the native function getValueOIDTestMIB is declared in a different class than what my main() function is declared in.
    For example something like this will work:
    class Main
    public native void getValueOIDTestMIB
    static {
    System.loadLibrary("libsnmp++");
    //............some more functions etc.............
    public static void main(String[] args)
    //............some more stuff...........................
    But when I declare this:
    public native void getValueOIDTestMIB
    static {
    System.loadLibrary("libsnmp++");
    outside the class, in another class within the same package, nothing happens.
    I am probabily doing something stupid. Can somebody help or give me some guidance to where I should look. I come from a C++ background, not a guru in Java.
    Thanks

    You need to run javah and give it as a parameter the full class name of the class which contains the native methods.
    For example (if your class is called A and its package is a.b.c)
    javah -jni a.b.c.A

  • How to use a C function as a native method

    Could anybody tell me how to use a C function as a Java native method.
    Thanks

    Read all about it.
    You'd probably have to write a little wrapper (using JNI) that passes
    parameters and return values around between your C function and
    the JVM.
    kind regards,
    Jos

  • How call native method without code modification

    "How to access third party's C API from Java Using JNI without modifying the C code"

    Unfortunately, the only way I know of doing this is to use a pass-through DLL that you write. Logically it
    looks like this:
    Java App (written by you)
    |
    v
    Java native methods (written by you)
    |
    v
    JNI DLL (written by you)
    |
    v
    3rd party DLL
    It adds a little bit of overhead, but it's not too big of a deal. Check out the JNI tutorial on how to write this.
    Bryan

Maybe you are looking for

  • How to change country of Nokia Music store

    How can I change the country of the Nokia Music store? Currently on my Lumia 920 it shows up as US even though all country settings are set to Switzerland (both LiveID and Nokia Account). The Windows store is set to Switzerland but I can't find a way

  • Skype 5: Skype can't connect

    I'm trying to log into Skype 5 but it says "Skype can't connect"... How do I fix that? Solved! Go to Solution.

  • Set stage size?

    Is there a way to have the stage size change during this one section that is vertically longer? I'd prefer not to have the html scrollbar through the whole site, but I need overflow to show for this one page of the Flash website (website is one long

  • Current objectives and next yearu2019s objectives in one appraisal document

    I keep coming across the requirement for an employee to be able to record comments and scores for his/her current objectives and at the same time to document his/her objectives for the next year in the same appraisal document. The SAP standard proces

  • Pro 7.2.3 crashes with korg microkontrol

    Please excuse newbie gaffs... But Logic Pro 7.2.3 crashes when I try to use some factory modes with my Korg microkontrol. >Pots>Instrument Edit, Plug-In Edit, and Plug-In Setting all send it off the rails, other modes work fine. I've installed Pro Ap