Java Bridge - Calling Java methods from Cocoa(Objective-C)
Java Bridge - Calling Java methods from Cocoa(Objective-C)
- Subject: Java Bridge - Calling Java methods from Cocoa(Objective-C)
- From: Saratchandra Kongara <email@hidden>
- Date: Sun, 8 Dec 2002 12:25:53 -0500
Hello,
I wrote a small java program that converts a RTF file to a HTML file. I
am trying to call the methods in this class from a Cocoa Objective-C
program.
Here is how I am integrating this in to my Cocoa (Objective-C) App.
// HTMLConversionTool.java
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import javax.swing.text.rtf.RTFEditorKit;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.DefaultStyledDocument;
public class HTMLConversionTool {
private HTMLEditorKit htmlKit;
private DefaultStyledDocument tempRTFDoc;
private RTFEditorKit rtfKit;
public HTMLConversionTool() {
htmlKit = new HTMLEditorKit();
tempRTFDoc = new DefaultStyledDocument();
rtfKit = new RTFEditorKit();
}
public static void main (String args[]) {
HTMLConversionTool converter = new HTMLConversionTool();
}
public String echo(String input) {
return input;
}
public void convert(String inputFile, String tempFile) throws
Exception {
File file = new File(inputFile);
rtfKit.read(new BufferedReader(new InputStreamReader(new
FileInputStream(file), "ISO-8859-1")), tempRTFDoc, 0);
Writer writer = null;
writer = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(tempFile), "ISO-8859-1"));
try {
htmlKit.write(writer,tempRTFDoc,0,tempRTFDoc.getLength());
writer.flush();
} catch (Exception ex) {ex.printStackTrace();}
finally {
if (writer != null)
writer.close();
}
}
}
I added an echo method just for testing the communication between
Objective-C code and Java code using the bridge.
In one of my methods in Objective-C code I do this
id converter=[[NSClassFromString(@"HTMLConversionTool") alloc] init];
NSString *result = [converter echo:@"How are you?"];
NSLog(@"response from converter: %@ ", result);
[converter release];
I also created a HTMLCoversionTool.h file in the Cocoa Project
// HTMLConversionTool.h
#import <Foundation/Foundation.h>
@interface HTMLConversionTool : NSObject
{}
- (NSString *)echo:(NSString *)value;
@end
I added these settings in Target Settings (Settings->Expert View) in my
Cocoa App.
NSJavaNeeded = YES
NSJavaPath = HTMLConversionTool.jar
NSJavaRoot = Contents/Resources/Java
I created a new group "Java" under Resources and added the
HTMLConversionTool.jar to this group in PB using the Project->Add Files
menu option.
I also added a copy files build phase to my project and dragged the
HTMLConversionTool.jar file from the Files->Resources->Java group to
the path field. This is how my Copy Files section in Build Phases reads
Where: Resources Subpath: Java
copy only when installing is unchecked.
Files: /Users/sarat/Workspace/Java
Projects/HTMLConversionTool/build/HTMLConversionTool.jar
Output from my NSLog()
response from converter: (null)
I checked the Application package contents after the build and it does
contain the jar file in the appropriate folder. Is there a file where I
can check for these settings (NSJavaNeeded, NSJavaPath, NSJavaRoot)? I
don't see them in info.plist.
Any help is appreciated. Thanks in advance.
Regards
Sarat Kongara
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.