Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>cls is probably invalid. I presume your program would have crashed earlier if 'cres' was null.</p> <pre><code>int main() { JVMCreationResult* cres = CreateJavaVM(); if(!cres) return -1; JavaVM* jvm = cres-&gt;jvm; JNIEnv* env = cres-&gt;env; jclass cls = env-&gt;FindClass("Main"); if(env-&gt;ExceptionCheck()) { // ClassNotFoundException ? env-&gt;ExceptionDescribe(); env-&gt;ExceptionClear(); } if(!cls) return -2; // this I think is your problem jmethodID mid = env-&gt;GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V"); // the evil line } </code></pre> <p>Are you sure your classpath has been specified correctly ? Hopefully FindClass("Main") will find a default package class. Anyhow tell us the return value if your C/C++ main() now.</p> <p>It is possible for the "JavaVM* jvm = cres->jvm;" to be optimized away, since "jvm" is never referenced and the statement "cres->jvm" has no side effects. Some commenters state it should crash on this, hmm yes maybe, if code was generated and then executed. But a decent compiler might see it is a no operation.</p> <p>However the statement "JNIEnv* env = cres->env;" can not be optimized away, since the variable "env" is used later on. So we can only claim that if cres==0 then it would crash at or before this point in execution. Since "env" is used for FindClass() call then we know for sure that env!=0 and therefore cres!=0.</p> <p>I would guess you have a Class Path setup issue, FindClass() is not finding your class at runtime which is causing "cls==0" to be true. Which is my answer here.</p> <p>EDITED: I see what the others are claiming over 'cres' however that does not change my original diagnosis, but you still have a bug regarding 'cres', change the line to:</p> <pre><code>JavaVMCreationResult* cres = new JavaVMCreationResult; </code></pre> <p>I think you are lucky that cres is pointing somewhere (probably on the stack), you then copied the values into the local main() stack and used the values. But that doesn't make the technique correct as the initial memory that 'cres' is pointing to is random, so you are lucky that no crash occured but you did scribble on memory you should not have. By using the "cres = new JavaVMCreationResult;" this causes the pointer to be set to a known valid block of memory.</p> <p>If you want compiler assistance with this problem (i.e. it should show up a warning) try with MinGW "-Wall" and "-O2" options during compile. It should warn about uninitialized use of the variable 'cres'.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload