3

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 or imports elsewhere as follows:

$ ./j2objc Hello.java

Can anyone provide me with an example of how to translate an entire package assuming I have a folder named input which contains my com package which contains all of the sub-packages and Java files that I want to translate?

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147

2 Answers2

7

To build a whole project, I add the source root(s) to the -sourcepath, then use the find command to locate all Java sources. For example, to build Square.com's Dagger library:

$ export J2OBJC=~/j2objc   # change to wherever your distribution is
$ cd ~/src/dagger/core
$ $J2OBJC/j2objc -d build_output -sourcepath src/main/java \
    -classpath $J2OBJC/lib/javax-inject.jar \
    `find src/main/java -name '*.java'`

All the generated .h and .m files are now in the build_output directory, in subdirectories according to their package (like javac does). To compile all the .m files into a static library, I use:

$ cd build_output
$ j2objcc -c -I. `find . -name '*.m'`
$ libtool -static -o libdagger.a *.o
tball
  • 1,984
  • 11
  • 21
  • Thanks for that tip Tom. I want to add the translated code to an iOS application. It's the output of `j2objcc` (i.e. the Objective-C source files) I should be adding to my iOS code right and not the output of `j2objcc` (i.e. the static library)? – Adil Hussain Apr 01 '15 at 14:16
  • Ignore above comment @tball. I've found the ["Xcode Build Rules"](https://github.com/google/j2objc/wiki/Xcode-Build-Rules) page and the ["j2objc-sample-reversi"](https://github.com/tomball/j2objc-sample-reversi) sample project, and I've added the Java files to my iOS app accordingly. – Adil Hussain Apr 02 '15 at 13:18
2

If there is no better way built into run.sh, you could use find's -exec flag:

find <path to input folder> -type f -exec --preservedirs ./run.sh {} \;

Or, you could use xargs to do multiple files at the same type:

find <path to input folder> -type f | xargs ./run.sh --preservedirs

(You might also need to add -name "*.java" to the find arguments if there are non-Java files in your directories).

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Hi Andy, thanks for your suggestion. I'll give this a try. Have you used Google's Java to Objective-C converter? Looks like the 'run.sh' shell script was replaced by the 'j2objc' unix executable in recent versions. Have you used this? – Adil Hussain Mar 31 '15 at 10:18
  • Never used it, this is a totally generic solution to passing multiple file arguments to a script. – Andy Turner Mar 31 '15 at 10:19
  • Brilliant. Tried your suggestion Andy and it works. So until someone suggests an alternative way to do this with `j2objc`, this command works for me: `find input -type f -exec ./j2objc -sourcepath input -d output {} \;`. The folder `input` is where my `com` folder is containing the packages/files to be translated, and `output` is where the translated files are to be written. – Adil Hussain Mar 31 '15 at 11:19