Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

What does FindClass() return?

Thread view: 
aa@dsa-ac.de - 26 Nov 2006 20:52 GMT
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
Arne Vajhøj - 26 Nov 2006 21:00 GMT
> what is the return value of 'findClass' exactly? Is
> it a pointer to the memory which contains executable
[quoted text clipped - 3 lines]
> 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
aa@dsa-ac.de - 27 Nov 2006 09:46 GMT
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
Andreas Leitgeb - 27 Nov 2006 10:34 GMT
> I tried with the function 'DefineClass(...)' but I get always a
> NULL pointer with the following exception:
[quoted text clipped - 3 lines]
> '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.
aa@dsa-ac.de - 27 Nov 2006 10:51 GMT
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
Gordon Beaton - 27 Nov 2006 11:39 GMT
> I copy the contents of my 'testClass.class' into a variable of type
> byte[]. I have created this class file with java compiler version
[quoted text clipped - 4 lines]
> '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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

aa@dsa-ac.de - 27 Nov 2006 12:00 GMT
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[i]);
    }
}
...
// 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[i] = static_cast<jbyte>(byteField[i]);
...
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
Andreas Leitgeb - 27 Nov 2006 12:23 GMT
> 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 :-)
Gordon Beaton - 27 Nov 2006 12:29 GMT
>     char line[100];
>      while (NULL != fgets(line, 100, f))
>      {
>          for (int i = 0; i < 100; ++i)
>              byteField.push_back(line[i]);
>      }

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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

aa@dsa-ac.de - 27 Nov 2006 13:40 GMT
Hello,

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

Anahita


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.