Hey guys I need some help. The problem is my jni is not loading after changing package name. Before it was working pretty cool. I have tried to solve it but I can't. So please help me.
This is the error:
JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception 'java.lang.ClassNotFoundExceptio
in call to NewGlobalRef
from java.lang.String java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader, java.lang.String)
JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with
pending exception 'java.lang.ClassNotFoundException' in call to
NewGlobalRef from java.lang.String
java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader,
java.lang.String)
You had a pending exception when you called NewGlobalRef. When an exception occurs when executing JNI code, your app doesn't crash, but a "pending exception" is created. It is your responsibility to check for exceptions when doing JNI work which might throw an exception.
After a pending exceptions is created, only a handful of JNI methods are safe to call( mentioned here).
In your case, your app crashed because before you called NewGlobalRef after a pending exception was thrown. NewGlobalRef is not in the list of methods safe to call after an exception. However, you also see the cause of the pending exception: java.lang.ClassNotFoundException.
Most likely this happened because you tried to do something similar:
cls = (*env)->FindClass(env, "com/example/ndktest/SomeClass");
but then you changed the package name of SomeClass from com.example.ndktest.SomeClass to com.other.package.SomeClass. However, you probably didn't also change how you searched for the class..so you need to also update your FindClass(...) call to:
cls = (*env)->FindClass(env, "com/other/package/SomeClass");
Hope this helps
Related
while i am trying to run the following code iam getting ExceptionInInitilizerError instead of NullPointerException. why?
static
{
String s= null;
System.out.println(s.length());
}
Static blocks are the part of class code in java which are called when the class is loaded for the first time . If you carefully look at the exception you receive :
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at Main.Test.<clinit>(Test.java:8)
You have a NullPointerException wrapped in an ExceptionInitializerError.
Hence you received a NullPointerException and because that error occurred at a space where normal execution further was not possible , as a result you got ExceptionInitializaterError
When you use the static initializer block, errors are not handled as they would be in the rest of your code. When an error is thrown in the initializer block, the JVM throws java.lang.ExceptionInInitializerError. This will also occur when initialization of a static variable fails.
This is because loader is seeing error when initializing this class. If you see internal stack trace, you will find NullPointerException. But since it was the process of initializing class when loader saw the error, you are seeing this 'ExceptionInInitilizerError'.
I have this class
public class demo3 {
private static void sum()
{
}
}
when I tried to run this class, I expected the error to be java.lang.NoSuchMethodError main Exception in thread "main "
however, the output was a bit different and I got below message
Error: Main method not found in class demo3, please define the main method as:
public static void main(String[] args)
now this got my curiosity as to in which case I will get java.lang.NoSuchMethodError or in which case I will get the other error message.
You get the Main method not found message when public static void main(String[]) can't be found in the class that you've asked the JVM to start running. That is, the entry point of the overall program cannot be found.
You get the java.lang.NoSuchMethodError message if your (already running) code attempts to invoke a method on a class which was available at compile time, but not available in the version of the class you are using at runtime (for example, you compile against one version of the library, and then update the library jar without recompiling). This can occur at any point in the program.
There doesn't look to be anything in JLS which says that NoSuchMethodError can't be thrown, rather than the Main method not found; however, failing to write a main method (either entirely, or writing one with the wrong signature) is a far more common mistake than the "class changed after compilation" case, especially for beginners, for whom the NoSuchMethodError might be too cryptic. There is no harm in providing a more user-friendly message in this one case.
public void useSkill(Champion target, int skillIndex, boolean enemy) throws UtilitiesException {
if (champSkills[skillIndex].getManaCost() > this.currentMana) {
throw new RequirmentNotMetException("Not Enough mana");
}
I would like to throw a requirmentNotMetexception (which extends the utilitiesException) with message "not enough mana".
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type UtilitiesException
at eg.edu.guc.lol.game.champions.Tank.useSkill(Tank.java:27)
at eg.edu.guc.lol.game.champions.asfasf.main(asfasf.java:33)
I want to use exceptions to show the user that the champion has no mana instead of using an if/else statement checking the mana of the champion and printing not enough mana.
Somebody in the call stack has to handle the RequirmentNotMetException, or every method on the call stack has to declare that it throws the exception (or a parent), or the exception has to extend RuntimeException. Evidently, your main method is not declared as throwing it, and I wouldn't expect it to. So, check up the call chain, identify who should handle the exception, have that method handle it, and have everything in between declare itself as throwing it.
Your useSkill method looks fine. It declares the exception type in the throws clause of the method signature, so the problem is likely where this method is invoked. It looks to me like you're getting the error runtime, which might indicate that you have not recompiled all the classes using the Tank class after you have changed the signature. Still, you need to either catch the exception where you're using the useSkill method, or just pass it on by declaring those methods to throw that exception type as well.
Either you have not imported the UtilitieException or your UtilitiesException class has errors.
I am trying to use a PageAdapter. I found out that public Object instantiateItem( View pager, int position ) has been deprecated. So I am trying up update but ran into a problem. The new definition changes the deceration to public Object instantiateItem( ViewPager pager, int position ), when I do this and push it to my device the app crashed.
Here is my logcat output.
12-26 19:24:30.701: ERROR/AndroidRuntime(25431): FATAL EXCEPTION: main
java.lang.UnsupportedOperationException: Required method instantiateItem was not overridden
at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:175)
at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
So I added #Override to the method call, but when I compile it ,using maven, I get the following output that corresponds to my method.
Chronos/ChronosApp/src/com/kopysoft/chronos/view/ClockViewer.java:[67,4] error: method does not override or implement a method from a supertype
I am at a lose as what to do. Any advice would be greatly appreciated!
The entire code can be found here: http://pastebin.com/da5Kqcmg
The runtime code in PagerAdapter.instantiateItem() is throwing the exception because it wants you to override that. So make sure you are overriding the method that is throwing the exception. You probably just want to switch your code back to overriding the deprecated method since that's what your runtime library is expecting.
Might it be possible that you have an older runtime you are running with?
I have created my own UI component in Java. It has model and a few of model's methods can throw my exception called ModelException. I want to use this component in JRuby but I can't raise my ModelException:
raise ModelException # it cause TypeError: exception class/object expected
So I tried to create method throwing ModelException in Java and then invoke it in JRuby:
public class ScriptUtils {
private ScriptUtils() {
}
public static void throwModelException(ModelException e)
throws ModelException {
throw e;
}
}
but when I call throwModelException from JRuby I get:
org.jruby.exceptions.RaiseException: Native Exception: 'class app.ui.ModelException'; Message:
; StackTrace: app.ui.ModelException
...
Caused by: app.ui.ModelException
this native exception cannot be handled by Java code.
Any ideas how to throw Java exception in JRuby and catch it in Java?
This is a complete re-write of my original answer as I originally mis-read your question!
You can raise Java exceptions and catch them in JRuby but you need to be a bit careful how you call raise:
raise ModelException
Will cause a type error (as you saw) because to JRuby ModelException looks like a plain constant. Remember that in Ruby class names are constants. You can raise direct subclasses of Ruby Exception like this, e.g.:
raise RuntimeError
But I think such subclasses are a special case. Those that are actually Java classes you need to call with a constructor:
raise ModelException.new
Or whatever constructor/s you have for that class. An instance of ModelException, in JRuby, is a subclass of Exception because JRuby creates it as such, but the Java class itself isn't. All this is assuming that you have imported your ModelException class correctly.
As for your second example, I couldn't replicate that error at all. As long as I created the exception object correctly, as above, it worked and I didn't see any complaints about "Native Exception" at all. So I'm not sure what is happening there, sorry.