j2objc
A Java to iOS Objective-C translation tool and runtime.
J2ObjC
Using J2objc I have a JavaUtilList:
var javaArrayList:JavaUtilList = JavaArrayList()
I want to convert this list into an NSArray:
var arr:[String] = // ????
How can I convert a JavaUtilList into an Objective-C/Swift NSArray?
Source: (StackOverflow)
I am translating a big project from Java to objective-C. When there are primitive data types, they will be translated to JNI Types instead of objective-C data types, such as boolean -> jboolean.
However, it is written in the documentation that the mapping should be directly from boolean to BOOL.
What I expect is it could translate from boolean to BOOL directly. Is it possible?
Source: (StackOverflow)
I usejava.util.ResourceBundle
for i18n like this:
try {
resourceBundle = ResourceBundle.getBundle("Messages", locale);
} catch (MissingResourceException ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
String localizedString = resourceBundle.getString("key");
I want to create the plural forms are well. Like:
one object
two objects
How can I define plural forms of localized Strings?
Source: (StackOverflow)
there is Android Source I Need to translate to Objective C
but it shows error :
java -Xbootclasspath:$J2OBJC_HOME/lib/jre_emul.jar -jar $J2OBJC_HOME/lib/j2objc.jar j2objcc
Invalid layout of java.lang.String at value
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (javaClasses.cpp:127), pid=575, tid=4867
# fatal error: Invalid layout of preloaded class
#
# JRE version: (8.0_51-b16) (build )
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.51-b03 mixed mode bsd-amd64 compressed oops)
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/emaar/AndroidStudioProjects/TestBuild/app/src/main/java/com/emaar/app/testbuild/hs_err_pid575.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
Abort trap: 6
any things set (JDK 8, Maven, SDK, Android Studio, Gradle, J2OBJC, ...)
how to solve it ?
Source: (StackOverflow)
I am using j2objc to compile and use a java library in iOS. I followed the processes:
- http://j2objc.org/docs/Xcode-Build-Rules.html
- http://j2objc.org/docs/Required-Link-Flags.html
I do not get any build errors until I start importing the header files to use them:
#import "org/library/Class.h"
The files are not found. What I am missing?
On the other hand, I tried to use the manually translated library files (using the terminal j2objc commands). If I pùt the .h and .m files into the j2objc-dist/include folder they are recognized by the editor and I can use the classes without compile errors. But, when I try to build the project it finds errors of the type _OBJ_CLASS_$_Java. I tried to include the files to the compile list in Xcode and I verified the path of libjre_emul.a but I still get the error.
My library contains packages so it has multiple folders in a tree.
My preference will be to use the first method (original Java sources)
DATA FOR JAVA SOURCES CASE:
Java source files
Custom script:
/Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/j2objc -d ${DERIVED_FILES_DIR} -sourcepath ${PROJECT_DIR}/src/ ** \ --no-package-directories ${INPUT_FILE_PATH};
Output files:
${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.h
${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.m
Link Binary with libraries:
libicucore.dylib
Security.framework
libz.dylib
Linking - Other Linker Flags:
-ljre_emul -L /Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/lib -force_load /Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/lib/libjre_emul.a -l jre_emul -ObjC
Search Paths - Library Seargh Paths:
/Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/lib
Search Paths - User Header Search Paths:
/Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/include
My java library files are in Project_root/src. The tree looks like this:
root/
src/
org/
orekit/
data/
time/
...
apache/
...
In my ViewController.m file I try to import with the following line without success (file not found):
#import "org/orekit/data/DataProvidersManager.h"
Source: (StackOverflow)
We use J2ObjC and are trying to make the switch to Xcode 6's dynamic frameworks in order to incorporate Swift into the project. Simply adding use_frameworks!
to our Podfile works great in the case where we're just using J2ObjC, but the problem comes when adding other libraries that reference it.
In this particular example, I have two pods in my Podfile, J2ObjC
and any other pod that uses it. Let's call it whatever
. The current J2ObjC
podspec can be found here. Here's the podspec for whatever
:
Pod::Spec.new do |s|
s.ios.deployment_target = '8.0'
s.requires_arc = true
s.source_files = '**/*.{h,m}'
s.dependency 'J2ObjC'
s.xcconfig = { 'HEADER_SEARCH_PATHS' => '${PODS_ROOT}/J2ObjC/dist/include',
'LIBRARY_SEARCH_PATHS' => '${PODS_ROOT}/J2ObjC/dist/lib' }
end
So far so good. Now, if I want to reference a Java class, say JavaUtilArrayList
inside my app, I can do that.
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
JavaUtilArrayList *arrayList = [[JavaUtilArrayList alloc]init];
NSString *whatever = @"o hai";
[arrayList addWithId:[whatever copy]];
NSLog(@"%@", arrayList);
}
Awesome, J2ObjC
works inside my main target. But if I try to do the same thing inside whatever
, even though the compiler thinks everything is fine (J2ObjC
imports work, classes are available, etc.), I get a linker error:
#import "SomeClass.h"
#import "java/util/ArrayList.h"
@implementation SomeClass // Inside 'whatever'
-(JavaUtilArrayList *)arrayList {
NSString *aThing = @"o hai";
JavaUtilArrayList *myList = [[JavaUtilArrayList alloc]init];
[myList addWithId:aThing];
NSLog(@"%@", myList);
return myList;
}
leads to:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_JavaUtilArrayList", referenced from:
objc-class-ref in SomeClass.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've been going crazy reading the CocoaPods guides, man ld
, and the J2ObjC
guides and am still not getting anywhere. Has anyone built a pod on top of J2ObjC
and successfully installed it using use_frameworks!
? Any advice would be super appreciated.
Source: (StackOverflow)
I am attempting to get j2objc to run on a few of my java only modules in a gradle project. I am using the gradle plugin for j2objc but I don't believe that is the source of any problems. The module uses XMLUnit to detect differences in input files. XMLUnit includes a class org.xmlunit.builder.JaxbBuilder
which fails translation due to not seeing some javax classes which are omitted in j2objc, presumably because they fail to translate. In any case, my code does not use the JaxbBuilder
class so I created a dead code report to omit it. I know I can use proguard to generate a more complete set but for now, my focus is just on preventing this class from being attempted as I am still working on a gradle CI pipeline for all this. The report is titled dead_code.log
and contains only the following line:
org.xmlunit.builder.JaxbBuilder
The j2objc plugin configuration is specified as:
j2objcConfig {
autoConfigureDeps true
// Xcode workspace directory
xcodeProjectDir '../ios'
translateArgs '--dead-code-report', './dead_code.log'
translateArgs '--doc-comments'
translateArgs '-v'
finalConfigure() // Must be last call to configuration
}
And I can see in the log that the dead code arguments were added to the command line, but in execution I then see:
INEST: parsing jar:file:/Users/jenkins/.gradle/caches/modules-2/files-2.1/org.xmlunit/xmlunit-core/2.0.0-SNAPSHOT/b11b5e5d8e9f223af2fbaa93982d17b55899498/xmlunit-core-2.0.0-SNAPSHOT-sources.jar!org/xmlunit/builder/JaxbBuilder.java
error: org/xmlunit/builder/JaxbBuilder.java:19: The import javax.xml.bind cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:20: The import javax.xml.bind cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:21: The import javax.xml.bind cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:22: The import javax.xml.bind cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:23: The import javax.xml.bind cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:24: The import javax.xml.bind cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:25: The import javax.xml.bind cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:26: The import javax.xml.bind cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:27: The import javax.xml.bind cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:31: The import java.beans.Introspector cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:43: Marshaller cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:56: Marshaller cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:57: Marshaller cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:82: Marshaller cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:83: The method createDefaultMarshaller() from the type JaxbBuilder refers to the missing type JAXBException
error: org/xmlunit/builder/JaxbBuilder.java:87: JAXBSource cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:87: JAXBSource cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:87: Marshaller cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:92: JAXBException cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:93: DataBindingException cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:99: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:103: XmlRootElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:103: Bound mismatch: The generic method getAnnotation(Class) of type AnnotatedElement is not applicable for the arguments (Class). The inferred type XmlRootElement is not a valid substitute for the bounded parameter
error: org/xmlunit/builder/JaxbBuilder.java:103: XmlRootElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:106: The method createJAXBElement(Object) from the type JaxbBuilder refers to the missing type JAXBElement
error: org/xmlunit/builder/JaxbBuilder.java:108: The method createInferredJAXBElement(Object) from the type JaxbBuilder refers to the missing type JAXBElement
error: org/xmlunit/builder/JaxbBuilder.java:117: JAXBException cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:117: PropertyException cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:118: JAXBContext cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:119: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:120: JAXBContext cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:120: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:120: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:123: JAXBContext cannot be resolved
error: org/xmlunit/builder/JaxbBuilder.java:125: Marshaller cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:126: Marshaller cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:126: Marshaller cannot be resolved to a variable
error: org/xmlunit/builder/JaxbBuilder.java:130: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:133: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:136: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:137: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:137: The method createJaxbElementFromObjectFactory(T) from the type JaxbBuilder refers to the missing type JAXBElement
error: org/xmlunit/builder/JaxbBuilder.java:139: The method createInferredJAXBElement(T) from the type JaxbBuilder refers to the missing type JAXBElement
error: org/xmlunit/builder/JaxbBuilder.java:146: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:156: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:161: JAXBElement cannot be resolved to a type
error: org/xmlunit/builder/JaxbBuilder.java:174: Introspector cannot be resolved
Which I interpret to mean it tried to process the JaxbBuilder
class. Is there something in the configuration that I am missing?
Source: (StackOverflow)
I've used an older version of Google's Java to Objective-C (J2ObjC) converter previously (i.e. version 0.5.2) and it was straightforward to translate an entire folder of Java files to their equivalent Objective-C files (and to preserve the directory structure in doing so). I just had to run the following shell executable:
$ ./run.sh —-preservedirs <path to input folder>
I've just downloaded the latest version of J2ObjC (i.e. version 0.9.1) and it's not clear from the Getting Started page or elsewhere how I can translate an entire folder of Java files rather than just a single Java file using the 'j2obc' executable. The only example provided in the Getting Started page is to translate a single Java file which has no dependencies/imports elsewhere as follows:
$ ./j2objc Hello.java
Can anyone provide me an example of how to translate an entire package assuming I have a folder named 'input' in which is my 'com' package which contains all the sub-packages and Java files to be translated?
Source: (StackOverflow)
I am trying to use the J2objC plugin to convert a working java project. I have downloaded the latest version of the plugin and am using Eclipse Juno. I've got J2objC 0.8 on my machine. I have set an output path and have tried a few different settings within the plug-in. However, no files of any kind are written/copied/exported anywhere when I run the plug-in. The console reads as follows:
J2OBJC Conole
Source Directory: /Users/name/Desktop/folder/Workspace/projectfolder
Destination Directory: /Users/name/Desktop/folder/translatedjava/projectfolder
Export finished.
Source: (StackOverflow)
I'm trying to use guava with j2objc but I'm getting the error:
j2objc TestJava.java
translating TestJava.java
error: TestJava.java:1: The import com.google.common cannot be resolved
error: TestJava.java:10: Lists cannot be resolved
Translated 0 files: 2 errors, 0 warnings
while running:
j2objc Test.java
where Test.java
contains:
import com.google.common.collect.Lists;
import java.util.ArrayList;
public class Test {
public static void TestMethod() {
ArrayList<String> objects = Lists.newArrayList();
objects.add(0, "Hello world");
System.out.println(objects);
}
}
I've downloaded the lastest release 0.9.5 and added it to .profile
:
export PATH=$HOME/bin/j2objc-0.9.5:$PATH
What else do I need to do in order to use guava?
Thanks!
Source: (StackOverflow)
I'm having difficulty understanding why this is consuming memory.
I have tried;
- Allowing more time for ARC to clean up
- Creating a __weak copy of globals to pass
- Looked at using __bridge or __bridge_transfer but I don't believe this is appropriate.
- Making globals public and referencing it directly (works, but impractical)
This iOS Objective c thread is translated via j2objc 0.9.3 from a Java App.
@implementation Comms_StatusThread
- (void)run {
while (true) {
// Consumes memeory at aproximately 100k per 5 min
[S globals];
@try {
[JavaLangThread sleepWithLong:10];
}
@catch (JavaLangInterruptedException *e) {
}
}
This translated static singleton stores "globals" to be accessed from anywhere in the app (the real code stores many more classes & callbacks).
@implementation S
Globals * S_globals__ = nil;
+ (Globals *)globals {
{
if (S_globals__ == nil) S_globals__ = [[Globals alloc] init];
return S_globals__;
}
}
@end
Any help appreciated. I'm new to objective-c and ARC. I've read a fair amount on ARC, but still don't understand the cause of this memory consumption.
Thanks to Student T I tried the following.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(test:) userInfo:nil repeats:YES];
return YES;
}
-(void) test: (NSObject*) o {
[S comms];
[S globals];
}
This does not consume memory and I was planning to do this however tball's new answer (use j2objc @AutoreleasePool) is clearly the best option, so I'll start there.
Thank you so much for all your answers!
Source: (StackOverflow)
I am using j2objc.
I have 2 classes:
LabelPosition.java
Event.java
In Event I have references to LabelPostion
I could translate the standalone Class Labelposition without problems.
Packagestructure and the File .h & -m are created.
Now I try to translate the Event.java, which has references to LabelPosition as follow:
./j2objc --verbose -d objective-c -sourcepath . Event.java
I get errors:
error: Event.java:79: LabelPosition cannot be resolved to a type
error: Event.java:79: LabelPosition cannot be resolved to a type
error: Event.java:284: LabelPosition cannot be resolved to a type
error: Event.java:285: LabelPosition cannot be resolved to a type
error: Event.java:287: LabelPosition cannot be resolved to a type
error: Event.java:288: LabelPosition cannot be resolved to a type
Following screens shows my Folderstructur:
What am I doing wrong ?
Source: (StackOverflow)
j2objc is great to develop shared projects across iOS, Android and Web. For me it seems to be clear that it is a good practice to create four projects:
- shared code
- web
- ios
- android
Also the MVP seems to be reasonable for these kind of projects. When using this pattern I guess that the Model and the Presenter go into the share code project and the view goes into the platform specific projects. From my understanding transport mechanisms should go into the shared project as well or do they belong to each platform specific project?
Are there any best practices or recommended design pattern which are great for these type of projects which share a lot of code?
Source: (StackOverflow)
I am trying to get j2Objc working with the XCode build rules setup as explained in the j2Objc Wiki.
The problem that I am facing is that the custom build rule which I add for compiling the java files doesn't seem to get executed at all.
I have tried to add all kinds of simple build rules which will just echo something but nothing shows up in the build log.
The app is a simple single view iOS only application.
Can anyone help me out with figuring out why custom build rules will not work ?
Source: (StackOverflow)
Can you include expressions in the "Output Files" section of a build rule in Xcode? Eg:
$(DERIVED_FILE_DIR)$(echo "/dynamic/dir")/$(INPUT_FILE_BASE).m
Specifically, when translating Java files with j2objc, the resulting files are saved in subfolders, based on the java packages (eg. $(DERIVED_FILE_DIR)/com/google/Class.[hm]
). This is without using --no-package-directories
, which I can't use because of duplicate file names in different packages.
The issue is in Output Files, because Xcode doesn't know how to search for the output file at the correct location. The default location is $(DERIVED_FILE_DIR)/$(INPUT_FILE_BASE).m
, but I need to perform a string substitution to insert the correct path. However any expression added as $(expression)
gets ignored, as it was never there.
I also tried to export a variable from the custom script and use it in Output Files, but that doesn't work either because the Output Files are transformed into SCRIPT_OUTPUT_FILE_X
before the custom script is ran.
Source: (StackOverflow)