Getting Started

First, get the source and build it.

To translate a Java source file (Hello.java, for example):

public class Hello {
  public static void main(String[] args) {
    System.out.println("hello, world");
  }
}
j2objc Hello.java
translating Hello.java
Translated 1 file: 0 errors, 0 warnings

To compile the translated file:

j2objcc -c Hello.m

j2objcc is a wrapper script that invokes your C compiler (normally clang, also known as LLVM, Apple's C/C++/Objective-C compiler). To build the executable:

j2objcc -o hello Hello.o
./hello Hello
hello, world

j2objcc forwards whatever options you specify for the Objective-C compiler. For example, to translate and build with debugging symbols, use the -g flag:

j2objcc -g -o hello Hello.m

Frequently Asked Questions

When I run j2objcc, it complains that "Foundation/Foundation.h" isn't found.

If compilation fails because Foundation/Foundation.h isn't found, the problem is that the iOS SDK wasn't found (that's where that header is).

  1. Make sure you have Xcode installed.
  2. Install the command line tools by running xcode-select --install.
  3. Run xcodebuild -showsdks, which should show at least one SDK for OS X, iOS, and iOS Simulator.
  4. If that fails, delete the Xcode application and go to step 1.

What flags does j2objcc take?

The j2objcc script is just a wrapper around the Objective-C compiler, clang. Run man cc or man clang to list its options.

When compiling with j2objcc, my project's header (.h) files cannot be found.

The compiler needs to know the directory where the translated files reside, using -I <directory>. So if the files were generated with j2objc -d foo/bar ..., then the j2objcc command needs -Ifoo/bar. If no output directory was specified in the j2objc command, then -I. needs to be added.

How do I run on Windows or Linux?

J2ObjC is an iOS tool that is for development on Mac OS X. You cannot compile any translated code because it requires an OS X or iOS SDK from Apple, which requires its SDKs only be used on Macs.

However, since the J2ObjC translator is pure-Java, translation can be done on other systems. On Linux, the j2objc script should work unchanged. Windows use requires either CygWin, or invoking Java directly. To invoke the translator without the j2objc script, use the following where J2OBJC_DIR is the directory where the J2ObjC distribution file was unzipped:

java -Xbootclasspath:\lib\jre_emul.jar -jar J2OBJC_DIR\lib\j2objc.jar [j2objc-flags] [source files]