Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Returning a String after invoking JVM

Reply
Thread Tools

Returning a String after invoking JVM

 
 
Arjen
Guest
Posts: n/a
 
      04-06-2004
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
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      04-06-2004
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
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      04-06-2004
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
 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      04-07-2004

"Andrew Thompson" <(E-Mail Removed)> wrote in message
news:ux4j9svle9cn.wmmyzyi97k10$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      04-07-2004
On Wed, 07 Apr 2004 02:46:35 GMT, Liz wrote:

> "Andrew Thompson" <(E-Mail Removed)> wrote in message
> news:ux4j9svle9cn.wmmyzyi97k10$(E-Mail Removed)...
>> 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
 
Reply With Quote
 
Arjen
Guest
Posts: n/a
 
      04-07-2004
"Liz" <(E-Mail Removed)> wrote in message news:<fYJcc.85393$K91.188942@attbi_s02>...
> "Andrew Thompson" <(E-Mail Removed)> wrote in message
> news:ux4j9svle9cn.wmmyzyi97k10$(E-Mail Removed)...
> > 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
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      04-07-2004
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
 
Reply With Quote
 
Arjen
Guest
Posts: n/a
 
      04-07-2004
Gordon Beaton <(E-Mail Removed)> wrote in message news:<4073b41e$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error invoking JVM through JNI Bruce Java 2 02-13-2006 09:29 PM
JNI question: invoking JVM... Daniel Java 0 01-28-2004 04:40 AM
MS JVM and Sun JVM problem Young-Jin Lee Java 3 01-21-2004 04:25 AM
Different behavior for newStringUTF() for Sun JVM and IBM Jvm Lasse Java 1 01-05-2004 07:49 PM
Re: Handling both MS JVM and Sun JVM Kevin Hooke Java 2 09-02-2003 05:31 AM



Advertisments