Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How do I program in Java with Xcode 4.2.1?

I'm a student at Georgia Tech and in my 1331 course we are currently learning Java. The professor has stated that he doesn't care what IDE we use and recommended a few to get us started. I've used jCreator in the past, but it is Windows only, and so I installed jGRASP as my secondary option on my Mac.


However, in class I noticed that the professor was writing Java in xCode! And I'm not sure what role the terminal played, but I think it was executing the code.


Now I'm new to my Macs and I can't testify that his Xcode was or wasn't the most recent version, but I have version 4.2.1 and I can't figure out how to create a Java project with xCode, at least in the way that it will recognize my Java syntax errors. Since I'm trying to learn programming, I would rather learn with the xCode IDE if at all possible since at some point I will be learning Objective-C and C++.


Any help or advice would be appreciated. If xCode no longer supports Java, what Mac Java IDEs would you all recommend?


Thanks in advance!

MacBook Pro, Mac OS X (10.7.2)

Posted on Jan 13, 2012 3:01 PM

Reply
13 replies

Sep 14, 2017 10:00 AM in response to Disgaea

I'm a student computer science — for some reason they're making me do Java all of this year. On top of that, they forced me to work in eclipse... My eyes burned 😟


I heard you could run Java projects in Xcode, Apple listed it as one of the IDEs compatible with java on their own site.

Here, under "Java IDEs"


I still couldn't figure out how to run it though...


Then I found this:

https://github.com/HendrikNoeller/Xcode-Java-Template


I tried it and it just seems to work, real convenient!

Jan 13, 2012 6:03 PM in response to Disgaea

First, the good news. Your professor uses a Mac. You might consider asking for an Xcode template. That way, the professor will know you are a Mac user too <wink, wink>


Now the bad news. Xcode4 is really designed for iOS and Mac App Store development. You can use it for other types of projects, including Java, but you will have more work to do. Xcode3 is a far better general-purpose IDE. It is a bit of a pain to get Xcode3 running on Lion.


I was able to get Xcode4 to compile and run a Java project. Here is how to do it. If anyone can offer any improvements, please do so.

1) File > New > New Project > Other > External Build System

2) Give it some name and save it somewhere.

3) File > New > New File > Other > Empty

4) Give it a Java-friendly name. In my example, use "HelloWorld.java"

5) Copy the contents below into the file and save.

6) File > New > New File > Other > Empty

7) Save as "Makefile".

8) Copy the contents below into the file and save.

9) The "Run" (>) button should at least compile your Java now.


Now it gets tricky. You don't have to do the next part. You could just open a Terminal to your project directory and run "java HelloWorld" if you want. I strongly suggest that. You can also just type "make" and use Xcode purely as a text editor.

9) Project > Edit Scheme > Debug > Info

10) Executable > Other > type ^⌘g > type "/bin" > choose "sh"

11) Arguments

12) Set "Base Expansion on" whatever you named your target

13) Add an argument '-c "/usr/bin/java -classpath $(PROJECT_DIR) HelloWorld"' for this example.

14) Cross your fingers

15) Click the "Run" (>) button.


PS: I have no idea how to run the Java debugger in Xcode. But then, I have no idea how to run the Java debugger at all.


Here are some starter file contents:

HelloWorld.java:

public class HelloWorld

{

public static void main(String[] args)

{

System.out.println("Hello, World!");

}

}


Makefile:

# A simple makefile for a Hello World Java program


# Define a makefile variable for the java compiler

JCC = javac


# Define a makefile variable for compilation flags

# The -g flag compiles with debugging information

JFLAGS = -g


# typing 'make' will invoke the first target entry in the makefile

# (the default one in this case)

default: $(subst .java,.class,$(wildcard *.java))


# this target entry builds the Average class

# the Average.class file is dependent on the Average.java file

# and the rule associated with this entry gives the command to create it

#

%.class : %.java

$(JCC) $(JFLAGS) $<


# To start over from scratch, type 'make clean'.

# Removes all .class files, so that the next make rebuilds them

#

clean:

$(RM) *.class



Those indentations are true tab characters. Ugh!


Give it a shot.

Jan 13, 2012 7:13 PM in response to Disgaea

Java is not a first class citizen of Xcode anymore, so you should in my opinion stay with other Java IDE.

I prefer IntellijIDEA from Jetbrain, but its not free.

Eclipse is a standard in Java world and enterprise sector.

NetBeans from Oracle is a good choice too. Netbeans UI designer for Java is very pretty, but IntellijIDEA have a good one too.

You can make some comparsion at this page of Wikipedia.

Apr 28, 2013 3:28 AM in response to Arc676

Arc676 wrote:


I can't find the "java" UNIX executable file.

Well, you'll have to get one. As I mentioned in my user tip, "You will need to download a JDK from somewhere".


Why do you think that Java is such a terrible language? Is it because its unsafe?

It's a long story. I suppose the language itself isn't all that bad, it is just the cumbersome runtime and horrible user interface. The few decent Java programs I've seen have been pure servers or pure graphics - nothing in between. My primary complaint is the way it has been embraced by the consulting industry as a way to burn through billable hours. The language is so big that most programmers don't even know what is in it and spend half their time reimplementing things that are built in. Your Java team can spend weeks on coding and never get past exception handling. Three days before launch the entire thing gets delayed six months for need of a minor update to the hibernate library. I have seen all of these things in practice.

How do I program in Java with Xcode 4.2.1?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.