What does FindClass() return?

A

aa

Hallo,

what is the return value of 'findClass' exactly? Is
it a pointer to the memory which contains executable
code of the class? Can I feed this pointer myself with
a byte stream that contains a class file (*.class)? My
problem is this, that I don't have any JAR file to load
my classes from it, I get the executable class code
in a byte stream and want call some methods of it...

Hava anbody any idea?

Thanks in advance,
Anahita
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

what is the return value of 'findClass' exactly? Is
it a pointer to the memory which contains executable
code of the class? Can I feed this pointer myself with
a byte stream that contains a class file (*.class)? My
problem is this, that I don't have any JAR file to load
my classes from it, I get the executable class code
in a byte stream and want call some methods of it...

As the signature says then it returns a Class object.

The defineClass method can produce a class from a byte[].

Arne
 
A

aa

Hello,

I tried with the function 'DefineClass(...)' but I get always a
NULL pointer with the following exception:

Exception in thread "main" java.lang.ClassFormatError: Unknown constant
tag 0 in
class file testClass

'testClass' is a very simple class without any methods, etc.
Is my byte[] not correct? What is 'tag 0'?!

Thanks in advance,
Anahita
 
A

Andreas Leitgeb

I tried with the function 'DefineClass(...)' but I get always a
NULL pointer with the following exception:
Exception in thread "main" java.lang.ClassFormatError: Unknown constant
tag 0 in
class file testClass
'testClass' is a very simple class without any methods, etc.
Is my byte[] not correct? What is 'tag 0'?!

It looks like you are passing the source
instead of the compiled class file.

PS: btw, one cannot get both a NULL pointer and an exception.
If an exception is thrown, then nothing is "return"ed.
 
A

aa

Hello,

I copy the contents of my 'testClass.class' into a
variable of type byte[]. I have created this class
file with java compiler version '1.5.0'.

About the exception: It is really so, that a get
a NULL pointer. But I ask then JVM for occured
Exceptions with the method 'ExceptionOccurred()'
and if it retruns true (it is the case) I call
'ExceptionDescribe()' to get the message.

Any idea?!

Anahita
 
G

Gordon Beaton

I copy the contents of my 'testClass.class' into a variable of type
byte[]. I have created this class file with java compiler version
'1.5.0'.

About the exception: It is really so, that a get a NULL pointer. But
I ask then JVM for occured Exceptions with the method
'ExceptionOccurred()' and if it retruns true (it is the case) I call
'ExceptionDescribe()' to get the message.

For future reference, it would have been a good idea to say that you
are using JNI, not Java. It wasn't until this post that it became
clear to me which you were referring to, since java.lang.ClassLoader
has methods whose names are similar or identical to the JNI functions
you seem to be using. The replies you've gotten so far seem to assume
you're referring to the ClassLoader methods.

Instead of simply describing your problems, why don't you post the
code that isn't working. For example, we cannot possibly guess if your
byte[] or the call to DefineClass are correct, based only on your
vague descriptions.

/gordon
 
A

aa

Hello,

thank you for your advices! Here is my code and as you have
seen I use JNI in C++ to execute Java code.

....
// here I build my byte array, I read the contents
// of a class file and add them to byte array
....
typedef std::vector<char> BYTE_ARRAY
...
BYTE_ARRAY byteField;
FILE* f = fopen("D:\\Temp\\testClass.class", "r");
if (NULL != f)
{
char line[100];
while (NULL != fgets(line, 100, f))
{
for (int i = 0; i < 100; ++i)
byteField.push_back(line);
}
}
...
// try to define the class
...
BYTE_ARRAY::size_type len = byteField.size();
jbyteArray jByteField = mEnv->NewByteArray(static_cast<jsize>(len));
jbyte* jBytes = mEnv->GetByteArrayElements(jByteField, 0);
for (int i = 0; i < static_cast<int>(len); ++i)
jBytes = static_cast<jbyte>(byteField);
...
jclass jClass = mEnv->DefineClass("testClass", NULL, jBytes, len);
if (mEnv->ExceptionOccurred())
mEnv->ExceptionDescribe();
....

jClass contains here NULL and the following exception
has occured:

Exception in thread "main" java.lang.ClassFormatError:
Unknown constant tag 0 in class file testClass

I hope that you have now sufficient information to
see what is wrong.

Thank you in advance,
Anahita
 
A

Andreas Leitgeb

thank you for your advices! Here is my code and as you have
seen I use JNI in C++ to execute Java code.

FILE* f = fopen("D:\\Temp\\testClass.class", "r");

If you're on windoze platform, you need "rb", rather than "r"
char line[100];
while (NULL != fgets(line, 100, f))

You'd better use fread (rather than fgets), since class files
are not line-oriented, but binary.

Unlike fgets, fread will also tell you the number of
bytes actually read. (this is important at end of file,
so you don't generate trailing garbage)

I hope that you have now sufficient information to
see what is wrong.
yes, much better, now :)
 
G

Gordon Beaton

char line[100];
while (NULL != fgets(line, 100, f))
{
for (int i = 0; i < 100; ++i)
byteField.push_back(line);
}


Use fread(), not fgets(), to read your binary data. Also, your code
assumes (probably incorrectly) that exactly 100 characters was read
each time. Probably you have corrupted the class contents here.

I'd suggest that you try writing the resulting array to a file and
comparing it with the original before you attempt DefineClass(). Make
this part work first!

Note too that DefineClass() wants a jbyte* (i.e. native array of
jbyte), not a "java byte array" as you're using in the subsequent
code.

/gordon
 
A

aa

Hello,

thank you for usful hints! I could solve my problem,
the reason was actually 'fgets(..)' and the corrupt byte
array...

Anahita
 

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
474,270
Messages
2,571,102
Members
48,773
Latest member
Kaybee

Latest Threads

Top