Mon Apr 21, 2014
News
- 31 August 2014
- Don’t export Android project’s JNI headers.
- 21 April 2014
- Use GitHUB to host the update site.
- Update blog.
- Nov 20 2013
- Updated the plugin to reflect changes for J2ObjC 0.8.7Better progress display
- Easier classpath libraries usage
- Mar 31 2013
- Added feature to export the generated ObjectiveC Files to external directory.
- Better organization of console logs with colors and proper spacing
- Feb 13 2013
- Updated the plugin to reflect changes for J2ObjC 0.6
Source Code
The complete source code to the plugin is at https://github.com/hemantasapkota/j2objc-eclipse-plugin
Installation - Plugin
In Eclipse, Install a new software via the following update site:
Eclipse Juno/Kepler (4.2 / 4.3)
https://raw.github.com/hemantasapkota/j2objc-eclipse-plugin/master/update_site
Eclipse Indigo 3.7
https://raw.github.com/hemantasapkota/j2objc-eclipse-plugin/3.7/update_site
Installation - J2OBJC Compiler
To be able to use the plugin, you should have the J2ObjC compiler in your system. Download it from http://code.google.com/p/j2objc/downloads/list and unzip it in your system.
Usage - Set the Path to the Compiler
Once installed, you should set the path to J2ObjC compiler in Eclipse global preferences.
Tutorial - Generate Objective-C from Java
Create a new java project called java2ios. In it, create a new class called BilingualHello.java with the package name java.ios. The contents of the file are as follows:
package java.ios;
public class BilingualHello {
String helloInEnglish;
String helloInSpanish;
public BilingualHello() {
helloInEnglish = "hello";
helloInSpanish = "hola!";
}
public void sayHelloInEnglish() {
System.out.println(helloInEnglish);
}
public void sayHelloInSpanish() {
System.out.println(helloInSpanish);
}
}
We will now use J2ObjC Eclipse Plugin to translate this source to Objective-C class. Right click on the project and click on the menu J2ObjC -> ToObjectiveC.
* *
You should see the command line feedback in the console. You can copy the command generated by the plugin and paste it in your terminal as well.
Once the translation is finished, you can see the generated files in your project.
The contents of the generated Objective-C class is below.
BilingualHello.h
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: /Volumes/MyFiles/Projects/WS/jptws/java2ios/src/java/ios/BilingualHello.java
//
// Created by hemantasapkota on 12/31/12.
//
#import "JreEmulation.h"
@interface JavaIosBilingualHello : NSObject {
@public
NSString *helloInEnglish_;
NSString *helloInSpanish_;
}
@property (nonatomic, copy) NSString *helloInEnglish;
@property (nonatomic, copy) NSString *helloInSpanish;
- (id)init;
- (void)sayHelloInEnglish;
- (void)sayHelloInSpanish;
@end
BilingualHello.m
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: /Volumes/MyFiles/Projects/WS/jptws/java2ios/src/java/ios/BilingualHello.java
//
// Created by hemantasapkota on 12/31/12.
//
#line 1 "/Volumes/MyFiles/Projects/WS/jptws/java2ios/src/java/ios/BilingualHello.java"
#import "java/ios/BilingualHello.h"
#line 3
@implementation JavaIosBilingualHello
- (NSString *)helloInEnglish {
return helloInEnglish_;
}
- (void)setHelloInEnglish:(NSString *)newHelloInEnglish {
helloInEnglish_ = newHelloInEnglish;
}
- (NSString *)helloInSpanish {
return helloInSpanish_;
}
- (void)setHelloInSpanish:(NSString *)newHelloInSpanish {
helloInSpanish_ = newHelloInSpanish;
}
#line 8
- (id)init {
if ((self = [super init])) {
helloInEnglish_ = @"hello";
helloInSpanish_ = @"hola!";
}
return self;
}
#line 13
- (void)sayHelloInEnglish {
#line 14
NSLog(@"%@", helloInEnglish_);
}
#line 17
- (void)sayHelloInSpanish {
#line 18
NSLog(@"%@", helloInSpanish_);
}
@end
Preferences
You can set the preferences for each java project by right clicking on it and selecting the properties menu.