java-dev
[Prev] Thread [Next] | [Prev] Date [Next]
Re: Xcode -> Java Native Interface -> Eclipse Matthew Grivich Fri Jul 11 12:06:25 2008
Greg,These diagnostics helped me get unstuck and solve the problem. Thanks. I also came to the conclusion that java support in Xcode is even more half baked than I previously believed. For the reference of future readers (and my own records), I provide a full procedure for using Xcode to handle the C with a java wrapper side of the java native interface, and Eclipse to handle all of the other java. Unfortunately, I do not know an easier way to get native macintosh code to run within java.
Using Xcode 3.0, OS 10.5.2, Eclipse 3.2.2. With later versions this procedure will hopefully be simpler.
In advance, note that if you do not have reasonable grasp of C, Java, and Ant, this project will be difficult for you. If you know C and Java, Ant is fairly easy to learn from Apache's tutorial. Also see Sun's specification of the java native interface.
Note that in the following instructions, sometimes you right click, sometimes you click, sometimes you double click, sometimes you expand a tree, sometimes you select a tab or a menu item. It is not always clear which is correct, so try all of them if necessary.
In Xcode:1) File -> New Project -> Java -> Java JNI Application -> XProjectName. 2) Run -> Console.
3) Build and Go. Verify that console output is: [Session started at 2008-05-19 19:23:14 -0700.] Started applicationjava.library.path = /Users/snld/xcode projects/XProjectName/build/Release:.:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
Instance created shared_function called with Hello World ! Finished application. Answer is 42 The Debugger has exited with status 0.4) If you need frameworks, add them with Targets -> JNILib -> Link Binary With Library -> Right click -> Add -> Existing Frameworks. 5) Real programmers use packages, so lets make one, no matter how difficult. Right click src -> Delete -> Delete References
6) CLOSE THE PROJECT7) Find your src folder in finder. XProjectName.java and XProjectNamejnilib.c should be there. If not, start over and this time delete the references, not the files. 8) Create XProjectName/src/com/company/packageName. Move XProjectName.java and XProjectNamejnilib.c into there. 9) Reopen the Xcode project. XProjectNamejnilib.c will be in red, because you moved it. Right click it -> delete. 10) Right click XProjectName. Add existing files. Select the src folder. Ok. Don't copy files into destination folder. Reference Type: Default. Text Encoding: Unicode. Create folder References for any added folders. Do not add to any targets. We have to do that manually. The folders should show up blue (this means you created folder references). 11) Targets -> JNILib -> Compile Sources -> Right Click -> Add Existing File. Select XProjectNamejnilib.c from your package folder. Add to target JNILib, not XProjectName. 12) Open src/com/company/packageName.java. Add package com.company.packageName; as the first line.
13) Open build.xml. Make the following changes:
<!-- generate the header files -->
<exec executable="/usr/bin/javah">
<arg line="-classpath '${jarfile}' -force -d
'${curr_header_dir}' *com.company.packageName.*XProjectName"/>
</exec>
<target name="run" depends="install">
<java classpath="${jarfile}"
classname="*com.company.packageName.*${ant.project.name}" fork="true">
<sysproperty key="java.library.path" value="${dist}"/>
</java>
</target>
14) To PackageNamejnilib.c change to #include
"*com_company_packageName_*XProjectName.h" to the top.
Change JNIEXPORT jint JNICALL Java*_com_company_packageName_*XProjectName_native_1method(JNIEnv *env, jobject this, jstring arg)
15) Double click Executables -> XProjectName. Arguments tab. Change Argument field to -cp XProjectName.jar com.company.packageName.XProjectName
16) Run -> Console.
Do you feel lucky?17) Build and Go. Verify that the output is wrong. Go back and figure out what went wrong and try again.
Useful tips:Sometimes the build gets in a funny state, and needs to be cleaned. Build -> Clean All targets. If this generates a lot of errors, that is a good thing because it means that the builder is paying attention.
javah (from the ant build) creates the header file com_company_packageName_XProjectName.h and puts it in the build/release/headers directory. The prototypes here must match those in your testjnilib.c file.
The ant build only goes up to the install target when you use the build command in xcode. To use ant fully, you must go to the command line, and type ant run (in the folder of build.xml). It is often easier to get ant working outside of xcode than inside it.
From the terminal: nm libXProjectName.jnilib shows you its contents. Package names should be included.
Unsatisfield link Error means that your java code cannot find the native method or library in question. Usually this is a naming or pathing problem.
Exception in thread "main" java.lang.NoClassDefFoundError: com/company/packageName/XProjectName means that your java initialization is not pointing to the main method correctly. Check step 15, or the run line of your ant build if you are using run ant.
On to Eclipse: 1) File -> New -> Java Project. Name it EProjectName, and hit okay.2) Right click EProjectName. New Package: com.company.packageName. Must have the same name as the package from xcode. 3) Right Click the new package name. New -> Class: XProjectName (the name of the Xcode project). Click Finish. Copy-Paste the contents from your xcode XProjectName.java to your eclipse XProjectName.java. 4) Right click EProjectName -> New -> Folder -> jnilibs. Use finder to copy the XProjectName/build/Release/libXProjectName.jnilib to this folder. 5) Right Click EProjectName -> Properties. Java Build Path. Source Tab. EProjectName/src tree. Double click Native Library Folder. Workspace. Select the jnilibs folder that you just made.
6) Hit the green Run arrow. Verify that it is good. Matthew Greg Guerin wrote:
Matthew Grivich wrote:I exited Xcode, and put my .java file in the src/edu/salk/snl/micrimap. I reopened Xcode and noticed that it had updated properly. I recompiled (no build errors) and ran. I still get the UnsatisfiedLinkError. I also tried moving Grabberjnilib.c to the same location. This time, I had to re-add the file to the project. Otherwise it was the same. Successful build and UnsatisfiedLinkError.Post the stack-trace of the UnsatisfiedLinkError. Also, where are the jar file and the jnilib file located when the error occurs? Finally, run the 'nm' command on the jnilib file (in Terminal): nm libYourNameHere.jnilib and post the output from that.Also, the automatically generated Grabber.h does not include any package information.That may be causing the problem.The prototypes are of the form JNIEXPORT jint JNICALL Java_Grabber_OpenCamera (JNIEnv *, jobject, jstring);That doesn't seem correct to me. JNI binds classes to C functions by name, and that name includes a package.I don't know if this correct, but the fact that the .jnilib works in Eclipse leads me to believe that it is.Can't tell. Insufficient information. For example, Eclipse might be working because it's using older class files and jnilibs. Can't tell unless you know exactly what files are in use when it works under Eclipse. You probably need to add diagnostic code that actually prints the entire class-name (anyObject.getClass()), so you can see whether you're using the class in the package or not. Other useful information is to print the java.class.path system property, along with the library path. Heck, just print every system property, since the extensions-dirs will also affect loading of libs. -- GG _______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list ([EMAIL PROTECTED]) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/mgrivich%40salk.edu This email sent to [EMAIL PROTECTED]
_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list ([EMAIL PROTECTED]) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/alexiscircle%40gmail.com This email sent to [EMAIL PROTECTED]
- Xcode -> Java Native Interface -> Eclipse Matthew Grivich
- Re: Xcode -> Java Native Interface -> Eclipse Michael Stringer
- Re: Xcode -> Java Native Interface -> Eclipse Matthew Grivich
- Re: Xcode -> Java Native Interface -> Eclipse Sandro Sabatini
- Re: Xcode -> Java Native Interface -> Eclipse Matthew Grivich
- Re: Xcode -> Java Native Interface -> Eclipse Moises Lejter
- Re: Xcode -> Java Native Interface -> Eclipse Greg Guerin
- Re: Xcode -> Java Native Interface -> Eclipse Matthew Grivich <=