Returning a String after invoking JVM

A

Arjen

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
 
C

Christophe Vanfleteren

Arjen said:
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?
 
A

Andrew Thompson

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.
 
L

Liz

Andrew Thompson said:
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
 
A

Andrew Thompson

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' ;-) ]
 
A

Arjen

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

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
 
G

Gordon Beaton

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
 
A

Arjen

Gordon Beaton said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top