NoClassDefFoundError

G

ginjasvinja

Hi everyone, here I come again with Visual Age for Java troubles. I
have created java class that uses JNI and everything is ok when I run
my class from command line.What the problem is: when I create the same
class using Visual Age I can not run it because of exception:

Exception in thread "main" java.lang.NoClassDefFoundError

Can anyone tell me what the problem is? Thanks!
 
K

klynn47

The NoClassDefFoundError occurs because the JVM can't find the .class
file for the program you are trying to run. Usually this is because of
the CLASSPATH.
 
G

ginjasvinja

But when I write code in Notepad and compile and run it from command
line,everything is ok. Problem is, when I write the same code in Visual
Age, that is not working.It looks like Visual Age making some troubles.
 
G

ginjasvinja

But when I write code in Notepad and compile and run it from command
line,everything is ok. Problem is, when I write the same code in Visual
Age, that is not working.It looks like Visual Age making some troubles.
 
C

Chris Uppal

ginjasvinja said:
Hi everyone, here I come again with Visual Age for Java troubles. I
have created java class that uses JNI and everything is ok when I run
my class from command line.What the problem is: when I create the same
class using Visual Age I can not run it because of exception:

Exception in thread "main" java.lang.NoClassDefFoundError

Does the exception refer to the class with native methods ? (I assume it does,
but you haven't said). What happens if you change the class definition so that
it doesn't use native methods (just dummy them out temporarily) or try to load
the library ? If that starts to work (it can suddenly find the class with only
those changes) then it seems to be a problem with either VAJ's internal JVM not
being able to find the DLL (which can be fixed my putting the DLL in the right
place -- whereever that might be ;-) or it may be that the internal JVM just
isn't able to do JNI. VAJ uses a non-standard JVM implementation which /might/
not support JNI. I can't remember one way or the other, but I wouldn't be
/too/ surprised if it didn't -- check the documentation unless you already know
for sure that it can.

-- chris
 
J

Jon Martin Solaas

ginjasvinja said:
But when I write code in Notepad and compile and run it from command
line,everything is ok. Problem is, when I write the same code in Visual
Age, that is not working.It looks like Visual Age making some troubles.

Yes, and like the previous poster states, that trouble seems to be the
classpath. So either fix VAJ classpath, or at least post it here so
people can have a clue about what is really wrong.
 
G

Gordon Beaton

I' ve searched this forum and I think I have found my error. In my
tutorial i read: "The name of the native language function that
implements the native method consists of the prefix Java_, the package
name, the class name, and the name of the native method."

The rule may be correct but I don't think it's complete. However
instead of trying to figure it out yourself, use javah properly and
you will get the correct names every time.
But I don't have the package name in any function name in header
file I' ve created, so in my C code, even I have package.

When you run javah, you must specify the fully qualified classname
(i.e. including the package name). The generated header file will show
you exactly how to declare your native methods.

E.g.:

javah my.package.MyClass

( -jni is no longer necessary )

/gordon
 
G

ginjasvinja

Thank you everyone. I think I've found my error. I think problem is
with my header file. From begining:

I wrote java code (VAJ 4.0) that uses JNI and compiled it fine.I did't
Know how to create header file from VAJ, so I' ve exported my project
from VAJ and made header file from command promt using javah -jni
command.I wrote native methods in C++ (Visual Studio 6.0). I included
java header file and I made dll successfully. In java I called
System.loadLibrary to load my dll.First I have tried to run java class
file from command line and that was successful try. When I' ve tried to
run it from VAJ I got the

NoClassDefFoundError.

Then I chaged System.loadLibrary with System.load and used absoluth
path to my dll. Now I am getting

UnsatisfiedLinkError.

I' ve searched this forum and I think I have found my error. In my
tutorial i read: "The name of the native language function that
implements the native method consists of the prefix Java_, the package
name, the class name, and the name of the native method." But I don't
have the package name in any function name in header file I' ve
created, so in my C code, even I have package. So I think it makes me
troubles. But I don't know how to fix it, because I am not familiar
with VAJ.

I hope you can understand my English and what my problem is, and would
be very thankfull for any advice.

I can not open any of links that you posted in this forum?!
 
G

Gordon Beaton

Here we go again VAJ troubles! I can't run the same code from VAJ.
Actually, I can run it and it seems like java goes fine but with no C
code executing. Result:

Some message from java before calling native....

Some message fro java after calling native...

I don't know what to do now, because I don't get any error. And I have
to use VAJ. Does anyone know what can be wrong?

First, I don't know VAJ so this is just a guess...

Where should printf output (i.e. stdout) go when running from VAJ? I'm
guessing you aren't in a console. You didn't get an exception, so my
theory is that your native method *is* being called, but you just
don't see the output. Try returning a value that the caller can check
instead.

/gordon
 
G

ginjasvinja

Ok, now everything is fine with header file. When I made new header
file with package name I also executed my java class file successfully
(from command prompt). I am getting just what I've wanted, something
like this:

Some message from java before calling native....

Native message...from C

Some message fro java after calling native...

Here we go again VAJ troubles! I can't run the same code from VAJ.
Actually, I can run it and it seems like java goes fine but with no C
code executing. Result:

Some message from java before calling native....

Some message fro java after calling native...

I don't know what to do now, because I don't get any error. And I have
to use VAJ. Does anyone know what can be wrong?
 
G

ginjasvinja

Thank you Gordon, you were right. VAJ has it's own console and there I
can see java answers. Whwn I returned some string from native method to
java and print it, I could see it also. So, native method is being
called but I can't see the output.
Now I have to inform better about VAJ.

Thank you everyone!
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top