PDA

View Full Version : Trouble Loading a dll


Peltito
07-25-2003, 06:38 PM
I've been browsing the forums for quite a bit now and decided to let you guys in on my problem :D.

The problem is this:

I'm developing a coordinate measuring system, basically it maps 3 dimensional objects to a display on the screen. Well, the Java app is supposed to interface with a PCI card in a windows environment. I perused the JNI pages on the java.sun.com website and followed it's instructions and have created the proper .dll for it. In order for my function calls to work I need to load the .dll. My problem is that it can't find the path. I read up, again, in the JNI pages on java.sun.com website and it says to use the commands 'set PATH=%path%;.' or to run my program with the parameter '-Djava.library.path=c:\blah\blah'.

So, the commands I was told to use are:

set PATH=%path%;.

java -Djava.library.path=c:\pathToLibrary programName

The first command doesn't work and the second one gives me a 'missing main' exception, which is weird because my App actually runs if I comment out the card interface stuff.

Help.... please?

imported_adam
07-26-2003, 07:21 AM
I think your first command is supposed to be set PATH=%path%;C:\whatever u want to add to your path\;

Peltito
07-27-2003, 11:44 PM
Actually, I think the period means the current/working directory. I apologize if that was unclear.

stuka
07-28-2003, 10:54 AM
In the second instance, does your path have spaces in it? If so, did you quote the "c:\path\to\card" part? It would appear that the path is confusing the interpreter, and spaces are notorious for that on Windows.

Peltito
07-28-2003, 12:49 PM
Actually there are no names in the path with spaces. The actual path is:

c:/apieproject/cms

I'm sure it's something stupid that I'm overlooking but I can't figure out what yet.

Btw, more info, if it's helpful.

We use JBuilder over here and like I said, when I set the path (set PATH=%path%;.) and then try to compile/run the thing in JBuilder it says can't find the .dll. When I go to command prompt and set the path (The java -Djava.library command thing) and than try to run it from there it says there's an exception, no main class def found error.

Thanks for all the help, please keep it coming and rescue me from this hell!;)

stuka
07-28-2003, 01:10 PM
Wait - "No main class def"? Do you have your project set up in a package? Can you show me the full command line you use, and any package name involved? That can often cause a problem.

Peltito
07-28-2003, 03:36 PM
The project is set up in package cms... but then again there are only 6 files and they are all in the same package and directory.

mainFrame.java - gui
cms.java - driver/application
cardInterface.java - definition of native functions required for JNI support.
cardInterface.cpp - copy/paste of .h file but fleshed out to call the actual functions from the card.
cardInterface.h - JNI produced header file for c++ functions
cardInterface.dll - .dll for the card produced from the .cpp

cms has the main in it and that's the one I run when I call
java -Djava.library.path=c:\apieproject\cms cms

The above command results in no main class def found error. If needed I'll try to post source code but I don't think I'm allowed.:suspect:

The .dll file is in the c:\apieproject\cms folder along with the source and .class files. I moved everything there to ensure that it would work since it didn't before. Results are the same.:(

Thanks for the help and patience.

stuka
07-28-2003, 04:37 PM
OK - I think I see part of the problem! You need to compile the application first. javac c:\apieproject\cms\*.java Run that, then try again, but use this command line: java -Djava.library.path=c:\apieproject\cms --classpath=. cms.cms Make sure and run that from the c:\apieproject directory. The CLASSPATH variable (or the --classpath command line switch) must contain the directory above where the package name starts, and you must specify package.name.mainClassName instead of just the main class name.

Peltito
07-28-2003, 08:15 PM
java -Djava.library.path=c:\apieproject\cms --classpath=. cms.cms
I tried this command in a number of ways. The way it is listed here didn't work and produced an

'unrecognized option: --classpath=.
Could not create the java virtual machine.'

error.

I tried the command again but like this:
java -Djava.library.path=c:\apieproject\cms classpath=. cms.cms
It gave me an

'Exception in thread "main" java.lang.NoClassDefFoundError: classpath=/'

error.

I tried the whole thing again but I removed everything from the cms directory and just placed them into the apieproject directory to make everything even simpler. Still no cigar but a new error. Command used was:
java -Djava.library.path=c:\apieproject classpath=. cms
'Exception in thread "main" java.lang.NoClassDefFoundError: cms (wrong name: cms/cms)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClasLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)'

Does the 'main' have to be in the class that loads the library?

stuka
07-29-2003, 10:30 AM
No, it doesn't. When a class is part of a package, you MUST reference it by package name and class name. For example, I have a class that is part of the com.specorp.ultralite package named KeyGenerator. When I want to run it, I have to specify java -cp c:\devel com.specorp.ultralite.KeyGenerator. The -cp or -classpath (my mistake on the two -'s) option tells the interpreter the root of your package location. Each 'dotted' section then specifies a directory - my class file resides in c:\devel\com\specorp\ultralite\. I understand your frustration - it took me a while to get it working too, but the directory structure and command line make sense once you sort of see how they work.

Peltito
07-29-2003, 02:40 PM
java.lang.UnsatisfiedLinkError: no c:/apieproject/cms/cardInterface in java.library.path

at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1312)

at java.lang.Runtime.loadLibrary0(Runtime.java:749)

at java.lang.System.loadLibrary(System.java:820)

at cms.cardInterface.<init>(cardInterface.java:18 )

at cms.centeredFrame.<init>(centeredFrame.java:30)

at cms.cms.main(cms.java:18 )


This is the newest error that I got from compiling/running with JBuilder. On the other hand I got the same error when compiling at the command prompt.

stuka
07-29-2003, 02:53 PM
Hey, at least it found your main class now! Is the DLL in the path it's showing there, or is it in the cms dir?

Peltito
07-29-2003, 08:42 PM
Well, I got it to find the .dll but now when it runs it says

'The application or DLL C:\ApieProject\cardInterface.dll is not a valid Windows image. Please check this against your installation diskette.'

Is this because I compiled the .dll on another windows2000 machine?

stuka
07-30-2003, 01:19 AM
Oooh - now that's one I'm not familiar with. It would almost seem like the DLL wasn't compiled correctly (or got corrupted in the transfer).

Peltito
07-30-2003, 04:32 PM
The dll is no longer corrupt. I can only assume that it was because I compiled for a different version of java on a different machine running a (slightly?) different version of Win2000. I got Visual Studio C++ on my machine and had to set all the weird/archaic environment variables to get the dll formatter/compiler to work.

Any which way, I hate to keep bringing this here but I'm kinda fed up with the stupid thing.:angry:

Error now is:

Exception in thread "main" java.lang.UnsatisfiedLinkError: initialize
at cms.cardInterface.initialize(Native Method)
at cms.centeredFrame.<init>(centeredFrame.java:31)
at cms.cms.main(cms.java:18)

cardInterface class contains all the native pseudo functions that call functions that are in the dll I created. The dll in turn calls the actual functions from the PCI card's dll. The unsatisfiedLinkError I can only assume means that it's not mapping the names correctly between it and my dll so it can't find the function? All the help is much appreciated.

Stuka, you have been great and I'm very thankful you've had the patience to stick through this. :sick:

stuka
07-31-2003, 10:18 AM
Glad I could help so far. I think you're right about the cause of the error - unfortunately I have no experience in actually writing JNI code, so I really don't know where to look here. One POSSIBLE cause is C++ name mangling - is your DLL interface wrapped in an 'extern "C"{}' block? If not, the compiler does its thing on the names, and that might break the names that the JNI code is expecting to find.

Peltito
07-31-2003, 01:05 PM
Yes, yes it is. Besides the actual code for the functions, most everything else is created by the machine.

The class (cardInteface in this case) with the native function definitions gets 'javah -jni' run on it. This creates a C/C++ header file which you copy/paste into a .c/.cpp file. You then go through and flesh it out with functionality. You then send this through a C/C++ compiler with the 'cl' command and a whole bunch of parameters and environment variables set to turn it into a dll.

Since, the names are created by the machine based off my function definitions in the java file, I'm not sure how it's breaking.

stuka
07-31-2003, 01:11 PM
Hmm...one way to check your DLL is to use the 'Depends' tool from Visual C++ (under Visual C++ Tools -> Depends). Open your DLL, and the lower of the 2 right-hand panes contains the names of all the functions your DLL exports.

Peltito
08-06-2003, 04:11 PM
Just a quick update for ya Stuka. I was able to eventually get the thing to compile the .dll correctly thus fixing my function mapping error. Still not sure exactly what caused it. App is near finished and we'll be putting a show on for the bigwigs this Thursday or Friday. Thanks for all the help. :D

stuka
08-06-2003, 04:19 PM
sweet! good luck!