public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* create an object with cni
@ 2013-06-24  9:37 Bucher Fabio
  2013-06-24 10:27 ` Bryce McKinlay
  0 siblings, 1 reply; 5+ messages in thread
From: Bucher Fabio @ 2013-06-24  9:37 UTC (permalink / raw)
  To: java

Hi

I just have a simple programm: One Java class, one C++ class.

Java:
public class Java_with_CNI {

    public Java_with_CNI(){
    }

    public void printHello(){
        System.out.println("Hallo Du, ich wurde durch CNI aufgerufen");
    }
}

and my C++ class:
#include <gcj/cni.h>
#include "Java_with_CNI.h"

class Hallo{
    int main(){
        Java_with_CNI test = new Java_with_CNI::Java_with_CNI();
        test.printHello();
        return 0;
    }
};


This is my first time i use C++, so I dont know whats wrong with my class. I just want to create a new Object (Java_with_CNI) and call the printHello()-method.
What is wrong with my class?

Thank you for your help and best regards
Fabio Bucher




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: create an object with cni
  2013-06-24  9:37 create an object with cni Bucher Fabio
@ 2013-06-24 10:27 ` Bryce McKinlay
  0 siblings, 0 replies; 5+ messages in thread
From: Bryce McKinlay @ 2013-06-24 10:27 UTC (permalink / raw)
  To: Bucher Fabio; +Cc: java

Fabio,

You need to initialise Java by calling "JvCreateJavaVM(NULL);" before
making calls from C++ into Java code.

See the CNI documentation (§ 11.16.2) for a complete example:
http://gcc.gnu.org/onlinedocs/gcj/Invocation.html#Invocation

Bryce

On Mon, Jun 24, 2013 at 10:37 AM, Bucher Fabio <fabio.bucher@ntb.ch> wrote:
> Hi
>
> I just have a simple programm: One Java class, one C++ class.
>
> Java:
> public class Java_with_CNI {
>
>     public Java_with_CNI(){
>     }
>
>     public void printHello(){
>         System.out.println("Hallo Du, ich wurde durch CNI aufgerufen");
>     }
> }
>
> and my C++ class:
> #include <gcj/cni.h>
> #include "Java_with_CNI.h"
>
> class Hallo{
>     int main(){
>         Java_with_CNI test = new Java_with_CNI::Java_with_CNI();
>         test.printHello();
>         return 0;
>     }
> };
>
>
> This is my first time i use C++, so I dont know whats wrong with my class. I just want to create a new Object (Java_with_CNI) and call the printHello()-method.
> What is wrong with my class?
>
> Thank you for your help and best regards
> Fabio Bucher
>
>
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* create an object with cni
@ 2013-06-24 12:54 Bucher Fabio
  0 siblings, 0 replies; 5+ messages in thread
From: Bucher Fabio @ 2013-06-24 12:54 UTC (permalink / raw)
  To: java

Thank you very much!

It works fine.

Best regards and have a nice week
Fabio Bucher

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: create an object with cni
  2013-06-24 12:17 Bucher Fabio
@ 2013-06-24 12:28 ` Bryce McKinlay
  0 siblings, 0 replies; 5+ messages in thread
From: Bryce McKinlay @ 2013-06-24 12:28 UTC (permalink / raw)
  To: java; +Cc: Bucher Fabio

On Mon, Jun 24, 2013 at 1:17 PM, Bucher Fabio <fabio.bucher@ntb.ch> wrote:
> Thank you, I can run this example and I put my code in there. Which is the right namespace to use, and which Class do I have to load with the JvInitClass()-method?

The namespace corresponds directly to the package name in Java. e.g.
"java.util" becomes "java::util". If your java class has no package
name then you don't need any namespace.

> Now the error message says:
> Test.cc:20:29: error: expected primary-expression before »)« token
> Test.cc:21:29: error: typ specification expected
> Test.cc:21:29: error: Java-object test is not with new allocated
> Test.cc:21:29: error: »,« or »;« expected
>
> Do I have to load my Java-class with the JvInitClass()-method? I think yes, but how is it done the right way?

Actually, no, you don't need to bother with JvInitClass in this case,
because calling "new" will ensure that it is initialised
automatically. The example only needs the JvInitClass() call because
it is accessing a static Java variable, System.out.

The correct syntax to reference your class would be:

&Java_with_CNI::class$  (This is the CNI equivalent of
"Java_with_CNI.class" in Java.)

Bryce

^ permalink raw reply	[flat|nested] 5+ messages in thread

* create an object with cni
@ 2013-06-24 12:17 Bucher Fabio
  2013-06-24 12:28 ` Bryce McKinlay
  0 siblings, 1 reply; 5+ messages in thread
From: Bucher Fabio @ 2013-06-24 12:17 UTC (permalink / raw)
  To: java

Thank you, I can run this example and I put my code in there. Which is the right namespace to use, and which Class do I have to load with the JvInitClass()-method? Now the error message says:
Test.cc:20:29: error: expected primary-expression before »)« token
Test.cc:21:29: error: typ specification expected
Test.cc:21:29: error: Java-object test is not with new allocated
Test.cc:21:29: error: »,« or »;« expected

Do I have to load my Java-class with the JvInitClass()-method? I think yes, but how is it done the right way?


#include <gcj/cni.h>
#include "Java_with_CNI.h"
#include <java/lang/System.h>
#include <java/io/PrintStream.h>
#include <java/lang/Throwable.h>

        int main(int argc, char *argv[]){
                using namespace java::lang;
                //using namespace ::Java_with_CNI;
                try{
                        JvCreateJavaVM(NULL);
                        JvAttachCurrentThread(NULL, NULL);
                        JvInitClass(&System::class$);
                        String *message = JvNewStringLatin1("Hello from C++");
                        System::out->println(message);


                       /* Here goes my Code */
20                      JvInitClass(Java_with_CNI);
21                      Java_with_CNI test = new Java_with_CNI::Java_with_CNI();
                        test.printHello();


                        JvDetachCurrentThread();
                }catch(Throwable *t){
                        System::err->println(JvNewStringLatin1("Unhandled Java exception:"));
                }
                //rtems_entry_point(void);
                //return 0;
        }

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-06-24 12:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-06-24  9:37 create an object with cni Bucher Fabio
2013-06-24 10:27 ` Bryce McKinlay
2013-06-24 12:17 Bucher Fabio
2013-06-24 12:28 ` Bryce McKinlay
2013-06-24 12:54 Bucher Fabio

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).