How to Set the Default Java Compiler Version in Xcode

Tagged:  
Java Tool Icon

I recently had the opportunity to write a quick command line application using Java. Instead of using Eclipse or BBEdit as by editor/IDE, I decided to use Xcode. I chose the Java Tool project template and got to work. In the course of my project, I wanted to spit some text out on the console. The Java language System library has a couple of functions for this purpose, print and println. Another approach is a feature of the C language in the form of a function "printf" that affords some easy formating options. When I tried to use printf I got the error message "cannot find symbol". This took me aback.

It turns out that printf was not in Java until Java 1.5 or so. You can use the command java -version to see what version you have on your system.

$ java -version
java version "1.5.0_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)

Apple has taken the conservative approach in Xcode and set the default library to Java 2 (1.2). It would be necessary to change my project to use the Java 1.5 library. This is easy. Just location the Build.xml in your project and find the target node (Project > target > javac) of the XML file. I found it about the 29th line.

<javac deprecation="on" srcdir="${src}" destdir="${bin}"
   source="1.3" target="1.2"
   includeAntRuntime="no"
   classpathref="lib.path" debug="${compile.debug}">
</javac>

I edited this file right in Xcode but you can use any text or XML editor. I changed the source and target to version "1.5".

<javac deprecation="on" srcdir="${src}" destdir="${bin}"
   source="1.5" target="1.5"
   includeAntRuntime="no"
   classpathref="lib.path" debug="${compile.debug}">
</javac>

This worked and I was able to get my project completed. I then needed to work on another project and had to repeat the process above. It occurred to me that there must be some way to fix this in a more permanent manner. As it turns out, Xcode has a very robust templating system. I found the Project Templates folder and the build.xml inside. I was able to make my changes to this file. Now every new project has these default settings.

/Developer/Library/Xcode/Project Templates/Java/Java Tool/build.xml

nosymbol.jpg
build_xml.jpg

Comments

Excellent. Thanks for the knowledge. I found another good articles about java manual at pdfph[dot]com

It seems to me that there is a Java language version flag which is set somewhere to 1.3. Presumably I want to set it to 1.6. However, I don't see where to set this.

The Java language System library has a couple of functions for this purpose, print and println. Another approach is a feature of the C language in the form of a function "printf" that affords some easy formating options. When I tried to use printf I got the error message "cannot find symbol". This took me aback.

erreauk

Douglas's picture

In Objective-C all strings are NSString Objects. In order to use the C functions, you would have to convert the NSString back into a C String. Fortunately there is a call for that.

In the next version of Xcode, Java development has been deprecated. You will still be able to compile your Java projects but the templates are gone for creating new projects. The message from Apple is clear to Java Developers: go away. The underlying Cocoa Bridge is gone that allowed Java to talk to Cocoa apps. This was what allowed WebObjects to make the transition from Cocoa to Java back in 1999. The choice for Java development is clear: Eclipse.

boediger

Douglas's picture

In the next version of Xcode, Java development has been deprecated. You will still be able to compile your Java projects but the templates are gone for creating new projects. The message from Apple is clear to Java Developers: go away. The underlying Cocoa Bridge is gone that allowed Java to talk to Cocoa apps. This was what allowed WebObjects to make the transition from Cocoa to Java back in 1999. The choice for Java development is clear: Eclipse.

Please support your argument.

Douglas's picture

I have used Eclipse as both a PHP IDE and as a WebObject/Java IDE. In both cases I found it to be extremely flexible and robust. I have not found another IDE that works as well for me. In the case of WebObject the architecture has allowed a community to spring up to fill the void left by Apple with the WOLips project.

Thx. This post really helped me.

Douglas's picture

This Build.xml gets overridden if you reinstall Xcode.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <strike> <img> <blockquote> <br> <img> <hr>
  • Lines and paragraphs break automatically.
  • Each email address will be obfuscated in a human readble fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

Clarifi

Domestic Airfare on Sale