Java function from c++

533 views
Skip to first unread message

Sergey Chernik

unread,
Feb 27, 2012, 8:59:20 AM2/27/12
to android-qt
How call java function from с++? I need in detailed description

Calugar George

unread,
Feb 27, 2012, 9:08:38 AM2/27/12
to andro...@googlegroups.com
I'm working right now on this, but i didn't succeed on this.

JavaVM* m_javaVM = (JavaVM*)QApplication::platformNativeInterface()->nativeResourceForWidget("JavaVM",0);
return always 0xaa00

and

UnionJNIEnvToVoid uenv; uenv.venv = NULL; qDebug()<<m_javaVM->GetEnv(&uenv.venv, JNI_VERSION_1_4);


return -2;


2012/2/27 Sergey Chernik <used...@gmail.com>

Sergey Chernik

unread,
Feb 28, 2012, 1:06:11 AM2/28/12
to android-qt
You can get JavaVM using "extern" before JavaVM, if you removed static
keyword from m_javaVM in qtmain_android.cpp.
Look: https://groups.google.com/forum/#!msg/android-qt/MesA5b2Bpv0/S1EmaNZP28IJ

It's work until I try to find my function Id, env->GetStaticMethodID
return always 0;

Calugar George

unread,
Feb 28, 2012, 9:30:16 AM2/28/12
to andro...@googlegroups.com
For me the error for
jmethodID playMediaFileMethodID = m_env->GetStaticMethodID(applicationClass,"playMediaFile", "(Ljava/lang/String;)V");

No method from QtApplicaiton.java can be found.

is:

D/Qt (11827): Java Class is: 0x466fc570

W/dalvikvm(11827): threadid=9: thread exiting with uncaught exception (group=0x40020c18)

E/AndroidRuntime(11827): FATAL EXCEPTION: Thread-15

E/AndroidRuntime(11827): java.lang.NoSuchMethodError: playMediaFile

E/AndroidRuntime(11827): at dalvik.system.NativeStart.run(Native Method)

geo...@wesell.ro

unread,
Feb 28, 2012, 12:54:08 PM2/28/12
to andro...@googlegroups.com

I successfully call Java function from Qt/C++. I will create a little tutorial in few hours. I don't know if this is the best approach, but at least it works and can be used for various tasks.

--

Sent from my Nokia N9

Nilson Aguiar

unread,
Feb 28, 2012, 1:06:53 PM2/28/12
to andro...@googlegroups.com
Thanks CMGeorge I'm new in Qt and I will apreciate your tutorial!

Calugar Mircea - George

unread,
Feb 28, 2012, 3:05:50 PM2/28/12
to andro...@googlegroups.com
I've added a post to my mini-blog related to changes made to use Java Class into the Necessitas.
http://blog.wesell.ro/2012-02/how-to-use-java-from-qtc-in-necessitas/

Best regards,
George Calugar

Endy Silveira

unread,
Feb 29, 2012, 12:51:11 PM2/29/12
to android-qt
Hello George,

I'm working over Necessitas 0.21, theres any way to do the samething
you do at necessitas 0.3 update 4? It would be REALLY great!

Bests,

Endy

On 28 fev, 17:05, Calugar Mircea - George <geo...@wesell.ro> wrote:
> I've added a post to my mini-blog related to changes made to use Java
> Class into the Necessitas.http://blog.wesell.ro/2012-02/how-to-use-java-from-qtc-in-necessitas/
>
> Best regards,
> George Calugar
>
> On 28.02.2012 20:06, Nilson Aguiar wrote:
>
>
>
>
>
>
>
> > Thanks CMGeorge I'm new in Qt and I will apreciate your tutorial!
>
> > Em terça-feira, 28 de fevereiro de 2012 14h54min08s UTC-3, CMGeorge
> > escreveu:
>
> >     I successfully call Java function from Qt/C++. I will create a
> >     little tutorial in few hours. I don't know if this is the best
> >     approach, but at least it works and can be used for various tasks.
>
> >     --
>
> >     Sent from my Nokia N9
>
> >     On 28.02.2012 08:06 Sergey Chernik wrote:
>
> >     You can get JavaVM using "extern" before JavaVM, if you removed static
> >     keyword from m_javaVM in qtmain_android.cpp.
> >     Look:
> >    https://groups.google.com/forum/#!msg/android-qt/MesA5b2Bpv0/S1EmaNZP...
> >     <https://groups.google.com/forum/#%21msg/android-qt/MesA5b2Bpv0/S1EmaN...>

Calugar Mircea - George

unread,
Feb 29, 2012, 12:59:11 PM2/29/12
to andro...@googlegroups.com
Sorry, but i don't have 0.21. If you have the qtmain_android.cpp, you
can send it to me and take a look over it.

On 29.02.2012 19:51, Endy Silveira wrote:
> Hello George,
>
> I'm working over Necessitas 0.21, theres any way to do the samething
> you do at necessitas 0.3 update 4? It would be REALLY great!
>
> Bests,
>
> Endy
>
> On 28 fev, 17:05, Calugar Mircea - George<geo...@wesell.ro> wrote:
>> I've added a post to my mini-blog related to changes made to use Java
>> Class into the Necessitas.http://blog.wesell.ro/2012-02/how-to-use-java-from-qtc-in-necessitas/
>>
>> Best regards,
>> George Calugar
>>
>> On 28.02.2012 20:06, Nilson Aguiar wrote:
>>
>>
>>
>>
>>
>>
>>
>>> Thanks CMGeorge I'm new in Qt and I will apreciate your tutorial!

>>> Em ter�a-feira, 28 de fevereiro de 2012 14h54min08s UTC-3, CMGeorge

Endy Silveira

unread,
Feb 29, 2012, 3:20:26 PM2/29/12
to android-qt
Got it, nothing of great diference, I can do it ;D

Thanks!

Endy

Ole-Johan

unread,
Mar 1, 2012, 8:43:23 AM3/1/12
to android-qt
Great stuff, George! The Java <-> Qt/C++ bridge is open once again, on
Necessitas alpha 3 update 4! :)

Just wanted to add a little something that I used this for:
In my java file (JavaManager from George's blog), I needed to run
startActivity(intent). And to do that I need access to a valid
activity, like m_mainActivity in the old Java/C++ bridge. I solved
that by modifying QtActivity.java, adding a static reference to the
QtActivity class itself, and a static method that returns this
reference. I set the static value to "this" in loadApplication().
After that, in the custom java class (JavaManager), I call the static
method in QtActivity, and I get my reference from which I can call
startActivity().

Hope this helps if anyone else needs to do the same!

Best regards,
Ole-Johan



On Feb 28, 9:05 pm, Calugar Mircea - George <geo...@wesell.ro> wrote:
> I've added a post to my mini-blog related to changes made to use Java
> Class into the Necessitas.http://blog.wesell.ro/2012-02/how-to-use-java-from-qtc-in-necessitas/
>
> Best regards,
> George Calugar
>
> On 28.02.2012 20:06, Nilson Aguiar wrote:
>
>
>
>
>
>
>
> > Thanks CMGeorge I'm new in Qt and I will apreciate your tutorial!
>
> > Em terça-feira, 28 de fevereiro de 2012 14h54min08s UTC-3, CMGeorge
> > escreveu:
>
> >     I successfully call Java function from Qt/C++. I will create a
> >     little tutorial in few hours. I don't know if this is the best
> >     approach, but at least it works and can be used for various tasks.
>
> >     --
>
> >     Sent from my Nokia N9
>
> >     On 28.02.2012 08:06 Sergey Chernik wrote:
>
> >     You can get JavaVM using "extern" before JavaVM, if you removed static
> >     keyword from m_javaVM in qtmain_android.cpp.
> >     Look:
> >    https://groups.google.com/forum/#!msg/android-qt/MesA5b2Bpv0/S1EmaNZP...
> >     <https://groups.google.com/forum/#%21msg/android-qt/MesA5b2Bpv0/S1EmaN...>
Message has been deleted
Message has been deleted

Nilson Aguiar

unread,
Mar 6, 2012, 12:10:47 PM3/6/12
to andro...@googlegroups.com
Hi George, can you send to us (in your blog or here) a sample project with the qtmain_android.cpp and the project, i'm having trouble to make it work

just a sample like a qDebug and a Log.e() just to discover how to setup the enviroment!



Best Regards,
Nilson Aguiar

Michał Kowalczyk

unread,
Apr 1, 2012, 9:55:30 PM4/1/12
to andro...@googlegroups.com
For those who still have some problems with communication between Necessitas application and Java, I wrote little article:
http://mkk.ekk.pl/?wpis=3
I hope that it's clear and will be useful.

Enrico Miglino

unread,
Apr 2, 2012, 1:18:48 AM4/2/12
to andro...@googlegroups.com

Great! Thank you :)

Tyler Mandry

unread,
Apr 2, 2012, 12:15:19 PM4/2/12
to andro...@googlegroups.com
FYI, I submitted a patch for the next alpha4 that opens up Java<->C++ (both ways), just as it was before alpha3. Basically it will be as if writing a standard JNI application, with your own JNI_OnLoad and all that. No more qtmain_android. BogDan says it will be in alpha4. See https://git.reviewboard.kde.org/r/104450/
--
Tyler Mandry

tomasl

unread,
Aug 28, 2012, 3:48:52 PM8/28/12
to andro...@googlegroups.com
Hi, has anyone been able to use this new Java <> C++ link now provided with alpha 4?  Both in alpha3 and previously, we hacked the qtmain_android.cpp and Application.java files to be able to do what we wanted (in alpha 3, we basicly followed this [1]). If anyone has an example or best practice, I think now is the time to show the world :)

Cheers, 

Reply all
Reply to author
Forward
0 new messages