Jump to content
NIRAV KAKU

Java class cannot be found

Recommended Posts

I have rather written a very simple class in Java which I would like to consume in Delphi. I have followed the instructions to the tee (to the best of my knowledge)... however, in the end, when I do try to consume the class method, I get 'Java class JWhatever could not be found.'

 

I have compiled the java to jar... jar into the classes.dex and deployed my classes.dex. I have written the wrapper correctly too and registered the types. 😕

 

I have attached the full source code with the dex files. Any help would be appreciated.

 

Nirav

JNI.rar

Share this post


Link to post

1. You don't need to add the classes.dex file to the project, nor the .java files

2. The UseAsJar.java file is missing the package directive which by convention is a domain in reverse order followed by a package identifier, e.g. com.mydomain.mypackage. Also, you do not need a main method, unless you're intending to run the jar separately.

3. Your import (android.JNI.UseAsJAR) is incorrect, since the signature for JUseAsJar does not match the package/class. It should be the package name followed by the class name e.g. com/mydomain/mypackage/UseAsJar. You don't need RegisterTypes, either; I believe that's a hangover from earlier versions of Delphi.

4. Once you've recompiled the .jar, add it to the project, in Project Manager under Target Platforms > Android. This saves having to manually deploy the classes.dex. You could then remove that manual addition.

 

I'd also recommend updating to Delphi 10.3.3

Edited by Dave Nottage
  • Like 1

Share this post


Link to post

1. You don't need to add the classes.dex file to the project, nor the .java files 
Yes. I know that. I just added them to the project file so that I could access them easily. That's all. 🙂 Besides, seeing all the files in the project brings a certain comfort when I am on uncharted waters. What I did not know is that you don't have to recompile into classes.dex! I didn't know you could simply add a JAR file in the libraries and wrap the header to directly use it! Thats amazing !! (unless I understood that wrong??)

 

2. The UseAsJar.java file is missing the package directive which by convention is a domain in reverse order followed by a package identifier, e.g. com.mydomain.mypackage. Also, you do not need a main method, unless you're intending to run the jar separately.
I don't understand what 'package directive' means or how to define it. Can you give an example of it using the current case? Yes, I know main is not needed. I was trying my hands at basics first so that got left in and I didn't bother to take it out. 😉

 

3. Your import (android.JNI.UseAsJAR) is incorrect, since the signature for JUseAsJar does not match the package/class. It should be the package name followed by the class name e.g. com/mydomain/mypackage/UseAsJar. You don't need RegisterTypes, either; I believe that's a hangover from earlier versions of Delphi.

Again, the signature has to be a copy (with '/' instead) of the fully qualified package directive? I saw Jim's video on this (pretty dated) so I thought this was a mandatory step. 😕

 

4. Once you've recompiled the .jar, add it to the project, in Project Manager under Target Platforms > Android. This saves having to manually deploy the classes.dex. You could then remove that manual addition.

Cool!

 

Thank you for all your advice!!

Share this post


Link to post

Thanks a ton Dave! I actually made decent headway!

 

1. Added the package directive with full path of the Java file.

2. Used the jar command and to compile into JAR file.

3. Added the JAR file to the project Project > Android (selected), it placed the JAR file in the Libraries folder.

image.png.4439dca19216ab98c3e8869a7a99ad05.png

 

4. I did get an error but then I figured out it was because of the location path (apparently dx requires strict path and package name mapping). It then created UseAsJAR-dexed.jar file automatically.

5. Changed the signature in android.JNI.UseAsJAR pas file.

6. Removed the RegisterType call (as you suggested).

 

But still I get the same error. 😞

 

So frustrating!

 

 

JNI.rar

Edited by NIRAV KAKU

Share this post


Link to post

JNI.rar did not include the compiled .jar file, nor did the project have .jar file added to libraries.

 

I updated the project to Delphi 10.3.3, compiled the UseAsJar.java file into UseAsJar.jar, added it to the project, zipped it and attached it to this reply. The project now works OK here.

 

You can "downgrade" the project to whatever version of Delphi you're using by right-clicking the "Libraries" node and click "Revert System Files to Default"

prjJNITest.zip

Share this post


Link to post

Hi Dave

 

Sorry being incognito for so many days. Since your last response, I was travelling and now am under self quarantine. Luckily no fever but still need to be careful. 

 

I will look into the code today! Thanks a ton! 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×