使用 J2Objc 将 Java 代码转换为 Objective-C

Converting Java code to Objective-C with J2Objc

我正在尝试使用 J2Objc 将 Java 中的 Compression Scheme for Unicode 实现转换为 Objective-C。在确保 Java 代码编译后,我已经成功下载并编译了 J2Objc 和 运行 命令 ./j2objec /SCSU/*.java。尽管如此,我还是遇到了一大堆错误,主要是说一些 类、变量和函数未定义:

error: SCSU/Compress.java:41: SCSU cannot be resolved to a type
error: SCSU/Compress.java:63: The method getCurrentWindow() is undefined for the type Compress
error: SCSU/Compress.java:77: The method selectWindow(int) is undefined for the type Compress
error: SCSU/Compress.java:105: EndOfOutputException cannot be resolved to a type
error: SCSU/Compress.java:105: EndOfInputException cannot be resolved to a type
error: SCSU/Compress.java:105: IllegalInputException cannot be resolved to a type
error: SCSU/Compress.java:107: The method getCurrentWindow() is undefined for the type Compress
error: SCSU/Compress.java:126: IllegalInputException cannot be resolved to a type
error: SCSU/Compress.java:134: EndOfInputException cannot be resolved to a type
error: SCSU/Compress.java:143: IllegalInputException cannot be resolved to a type
error: SCSU/Compress.java:165: SQ0 cannot be resolved to a variable
error: SCSU/Compress.java:171: dynamicOffset cannot be resolved to a variable
error: SCSU/Compress.java:171: dynamicOffset cannot be resolved to a variable
error: SCSU/Compress.java:173: dynamicOffset cannot be resolved to a variable
error: SCSU/Compress.java:181: EndOfOutputException cannot be resolved to a type
error: SCSU/Compress.java:216: EndOfOutputException cannot be resolved to a type
error: SCSU/Compress.java:218: Debug cannot be resolved
error: SCSU/Compress.java:219: The method getCurrentWindow() is undefined for the type Compress
error: SCSU/Compress.java:224: EndOfOutputException cannot be resolved to a type
error: SCSU/Compress.java:228: SQ0 cannot be resolved to a variable    
error: SCSU/Compress.java:231: dynamicOffset cannot be resolved to a variable
error: SCSU/Compress.java:231: dynamicOffset cannot be resolved to a variable
error: SCSU/Compress.java:233: dynamicOffset cannot be resolved to a variable
error: SCSU/Compress.java:238: staticOffset cannot be resolved to a variable
error: SCSU/Compress.java:238: staticOffset cannot be resolved to a variable
error: SCSU/Compress.java:240: staticOffset cannot be resolved to a variable
error: SCSU/Compress.java:245: Assert cannot be resolved to a type
error: SCSU/Compress.java:249: Debug cannot be resolved
error: SCSU/Compress.java:266: EndOfOutputException cannot be resolved to a type
error: SCSU/Compress.java:279: The method isCompressible(char) is undefined for the type Compress

日志继续...为什么我会收到此类错误,我应该如何解决这些错误?

您需要在 -sourcepath 中指定源所在的目录(j2objc 使用 Eclipse 编译器作为其前端,这就是此要求的来源)。我将这些源文件复制到 ~/Downloads/unicode,并使用了这个命令:

$ j2objc -d build -sourcepath ~/Downloads/unicode ~/Downloads/unicode/*.java translating /Users/tball/Downloads/unicode/Assert.java translating /Users/tball/Downloads/unicode/Compress.java translating /Users/tball/Downloads/unicode/CompressMain.java translating /Users/tball/Downloads/unicode/Debug.java translating /Users/tball/Downloads/unicode/Display.java translating /Users/tball/Downloads/unicode/EndOfInputException.java translating /Users/tball/Downloads/unicode/EndOfOutputException.java translating /Users/tball/Downloads/unicode/Expand.java translating /Users/tball/Downloads/unicode/IllegalInputException.java error: /Users/tball/Downloads/unicode/SCSU.java:241: The method reset in type SCSU can only set one of public / protected / private

最后是一个奇怪的错误,但是查看源码发现它确实是非法的Java:

SCSU.java:241: protected public void reset() { ...

解决这个问题(删除任一修饰符),你应该可以开始了。