Returning a String after invoking JVM

Discussion in 'Java' started by Arjen, Apr 6, 2004.

  1. Arjen

    Arjen Guest

    Hello,
    I want to invoke the JVM and call a method in a class which returns a string.
    I haven't found a usable example so far for doing this. Any suggestions?

    Arjen
    Arjen, Apr 6, 2004
    #1
    1. Advertising

  2. Arjen wrote:

    > Hello,
    > I want to invoke the JVM and call a method in a class which returns a
    > string. I haven't found a usable example so far for doing this. Any
    > suggestions?
    >
    > Arjen


    You can't. The main method in Java is declared to as void, so you can't
    really "return" anything.

    But what exactly are you trying to achieve? Wouldn't just writing to
    System.out do what you need?

    --
    Kind regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Apr 6, 2004
    #2
    1. Advertising

  3. On 6 Apr 2004 09:44:19 -0700, Arjen wrote:

    > I want to invoke the JVM and call a
    > method in a class which returns a string.


    public static void getString() {
    return "Here it is!";
    }

    Which is a really dumb question, unless
    you meant return a String on System.exit(-1),
    which, if you check the method signature,
    accepts an int, obviously not what you want.

    The only way to have output available after
    the exit of the VM, that I know of, is to
    write it to a file.

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Apr 6, 2004
    #3
  4. Arjen

    Liz Guest

    "Andrew Thompson" <> wrote in message
    news:ux4j9svle9cn.wmmyzyi97k10$...
    > On 6 Apr 2004 09:44:19 -0700, Arjen wrote:
    >
    > > I want to invoke the JVM and call a
    > > method in a class which returns a string.

    >
    > public static void getString() {
    > return "Here it is!";
    > }


    did you try to compile this, I didn't but
    it looks like you might get an error


    >
    > Which is a really dumb question, unless
    > you meant return a String on System.exit(-1),
    > which, if you check the method signature,
    > accepts an int, obviously not what you want.
    >
    > The only way to have output available after
    > the exit of the VM, that I know of, is to
    > write it to a file.
    >
    > --
    > Andrew Thompson
    > http://www.PhySci.org/ Open-source software suite
    > http://www.PhySci.org/codes/ Web & IT Help
    > http://www.1point1C.org/ Science & Technology
    Liz, Apr 7, 2004
    #4
  5. On Wed, 07 Apr 2004 02:46:35 GMT, Liz wrote:

    > "Andrew Thompson" <> wrote in message
    > news:ux4j9svle9cn.wmmyzyi97k10$...
    >> On 6 Apr 2004 09:44:19 -0700, Arjen wrote:
    >>
    >>> I want to invoke the JVM and call a
    >>> method in a class which returns a string.

    >>
    >> public static void getString() {
    >> return "Here it is!";
    >> }

    >
    > did you try to compile this, I didn't but
    > it looks like you might get an error


    (shrugs) I don't intend compiling it,
    so I doubt I'll get an error, though of
    course, you are correct.

    I doubt it was what the OP wanted in
    any case. [ And if they have a problem,
    I'll 'give them a full refund' ;-) ]

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Apr 7, 2004
    #5
  6. Arjen

    Arjen Guest

    "Liz" <> wrote in message news:<fYJcc.85393$K91.188942@attbi_s02>...
    > "Andrew Thompson" <> wrote in message
    > news:ux4j9svle9cn.wmmyzyi97k10$...
    > > On 6 Apr 2004 09:44:19 -0700, Arjen wrote:
    > >
    > > > I want to invoke the JVM and call a
    > > > method in a class which returns a string.

    > >
    > > public static void getString() {
    > > return "Here it is!";
    > > }

    >
    > did you try to compile this, I didn't but
    > it looks like you might get an error
    >
    >
    > >
    > > Which is a really dumb question, unless
    > > you meant return a String on System.exit(-1),
    > > which, if you check the method signature,
    > > accepts an int, obviously not what you want.
    > >
    > > The only way to have output available after
    > > the exit of the VM, that I know of, is to
    > > write it to a file.
    > >
    > > --
    > > Andrew Thompson
    > > http://www.PhySci.org/ Open-source software suite
    > > http://www.PhySci.org/codes/ Web & IT Help
    > > http://www.1point1C.org/ Science & Technology


    I should have been a bit more elaborate. I'm invoking the JVM from a
    c-program
    using JNI_CreateJavaVM.
    After invoking the JVM I'm looking for the class and the method I want
    to use.
    I can only find examples of calling methods that return integer and
    boolean but none which return a String.

    Arjen
    Arjen, Apr 7, 2004
    #6
  7. On 7 Apr 2004 00:44:21 -0700, Arjen wrote:
    > I should have been a bit more elaborate. I'm invoking the JVM from a
    > c-program using JNI_CreateJavaVM. After invoking the JVM I'm looking
    > for the class and the method I want to use. I can only find examples
    > of calling methods that return integer and boolean but none which
    > return a String.


    You invoke all methods in exactly the same way.

    Specify the correct signature when you use GetMethodID(), and invoke
    the method using CallObjectMethod(). If the method is static, use
    GetStaticMethodID() and CallStaticObjectMethod() instead.

    The method will return a jstring (reference to an instance of
    java/lang/String) from which you can extract the C style string (const
    char*) using GetStringUTFChars(). Don't forget to use
    ReleaseStringUTFChars() when you are finished using the C string.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Apr 7, 2004
    #7
  8. Arjen

    Arjen Guest

    Gordon Beaton <> wrote in message news:<4073b41e$>...
    > On 7 Apr 2004 00:44:21 -0700, Arjen wrote:
    > > I should have been a bit more elaborate. I'm invoking the JVM from a
    > > c-program using JNI_CreateJavaVM. After invoking the JVM I'm looking
    > > for the class and the method I want to use. I can only find examples
    > > of calling methods that return integer and boolean but none which
    > > return a String.

    >
    > You invoke all methods in exactly the same way.
    >
    > Specify the correct signature when you use GetMethodID(), and invoke
    > the method using CallObjectMethod(). If the method is static, use
    > GetStaticMethodID() and CallStaticObjectMethod() instead.
    >
    > The method will return a jstring (reference to an instance of
    > java/lang/String) from which you can extract the C style string (const
    > char*) using GetStringUTFChars(). Don't forget to use
    > ReleaseStringUTFChars() when you are finished using the C string.
    >
    > /gordon


    Thanx got it working!

    Arjen
    Arjen, Apr 7, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Hooke

    Re: Handling both MS JVM and Sun JVM

    Kevin Hooke, Aug 26, 2003, in forum: Java
    Replies:
    2
    Views:
    862
  2. Lasse
    Replies:
    1
    Views:
    680
    Jon A. Cruz
    Jan 5, 2004
  3. Young-Jin Lee

    MS JVM and Sun JVM problem

    Young-Jin Lee, Jan 20, 2004, in forum: Java
    Replies:
    3
    Views:
    562
    Mickey Segal
    Jan 21, 2004
  4. Daniel
    Replies:
    0
    Views:
    389
    Daniel
    Jan 28, 2004
  5. Bruce
    Replies:
    2
    Views:
    4,035
    Vitaly
    Feb 13, 2006
Loading...

Share This Page