How-to crossplatform java in Xcode?

Hi,
I just started my Java programming (new to programming) class tonight and I have a question or two. My college is a Windows only school and I would rather use my PowerBook to do the classwork. So, my question is: If I write a java program and compile it in Xcode will it work in windows as well? Do I have to change any lines of code to make it cross platform? I noticed in my textbook that all of the examples are in Windows format so, that got me wondering, if I write the code as it is in the text; will the program look like an OSX program? Meaning, will the control buttons be on the top left corner of the window.

You'll be fine using Xcode. Everything should compile just fine on both platforms as long as you don't call any platform-specific code, which I doubt you will in an intro programming course.

Yes, by default, you'll get the Mac OS look and feel when running a Swing application on a Mac. Be aware that platform-specific look and feels don't carry over to other platforms. It will look like a Mac app when you run it on your Powerbook, and it will look like a Windows app on Windows. If you don't want that, you can explicitly declare a look and feel that will work on both platforms - like Metal, for example. That will give you a consistent UI whether you're using Windows and Mac (though the control buttons will still be on the top left of the window on Mac OS; that's an OS thing and you can't change that).

To be on the safe side, it wouldn't hurt to test your programs on a Windows machine before you turn them in just to verify everything looks okay. Chances are that in an intro Java class, you won't run into any issues, but better safe than sorry.

thanks for the info. Ever since I switched to Mac this past April, I try not use Windows as much as possible. It's funny, my instructor said that of all his years of teaching, I'm the first Mac user he has had.

Figuring you won't even touch GUI programming in a beginner course, you should be just fine.

Curious, what program is your professor pushing to code Java in Windows. If Eclipse/Netbeans/ Java Studio - they are also available for OS X.

If just plain old text editor with command line compiling, it wouldn't hurt doing the same so you can learn the intricacies of the SDK.

Actually, while I think XCode is great for Carbon/Cocoa/Obj-C development - I would recommend Eclipse for Java Development - if you must use an IDE - its quickly becoming the industry standard for enterprise Java development.

textpad is what they will be using. Actually this is a comprehensive course. going from command line to applets, servlets, and GUI. The downside is that all of this will be done in only 10 weeks.

Click to expand...

If you want to keep it simple, I would recommend TextWrangler (free), it's a nice code editor (and plain-text editor in general) with syntax highlighting and great search and replace features. I can't believe they're using TextPad...painful. Xcode is a great development tool, but may have a lot of junk in there you don't want to be dealing with when learning the basics. If your professor is teaching using only text files and command-lines, I'd use TextWrangler and Terminal, you can always go to Xcode or Eclipse later if you want. TextWrangler will also deal very well with any auxiliary text files you may need, such as HTML, XML, or Ant build files.

Finally we were able to start coding tonight but I ran into a few problems. For our first project we were to code a comand line application. So, I start up Xcode and I notice there are a ton of java templates to choose from. Well, I didn't know which one to use so I just used the Textpad app in windows Now, I get home and take the file I created, put on my mac, and tried to figure some things out. I double clicked my .java file and it opens in Xcode but nearly all of the options are greyed out. Such as compile, debugg, ect. What's going on?!! I even tried TextWrangler and the same thing is happening. I went to Apple's Dev page and got J2SE 5.0 and then it tells me in order to use 5.0 I have to open a target (no idea what that is) and click this and that and now I'm ready to pull my hair out. I appologize if these questions seem obvious and/or bothersome to you all. I tried to do this on my own for about 3 hours and I have decided I need some help.

Xcode is powerful and flexible, but it isn't the easiest IDE to get used to.

I would have recommended Netbeans before version 3.6 for cross platform Java development, but as of Netbeans version 4.0, they started doing file management in a very weird way. I haven't understood it yet, so I cant' recommend it fully. I still think you will find it easier for small projects than Xcode is. Netbeans version 4.0 was quite well integrated into OS X, too, as opposed to version 3.6, so you might want to give the current version of Netbeans (4.1) it a try. I haven't tried the latest version, yet.

One tip (for version 4.0 at least), Netbeans is quite a resource hog, but on OS X you can make it go a lot faster by disabling text antialiasing.

Xcode is definitely not easy to jump into. I still haven't sat down and figured it out for myself, so I can't really help out there.

Netbeans is nice. I use Eclipse. Play with one or both of those and see if they work better for you. Feel free to post here again with questions on either of those, and I'll help as best I can (I'm sure others will too).

If neither one works out, perhaps a simple text editor and Terminal combo will do the job.

I double clicked my .java file and it opens in Xcode but nearly all of the options are greyed out.

Click to expand...

That's because you need a project first; you can open a single source file for viewing or editing, but you can't compile it in Xcode until you create a project, configure build settings, and add source files to it.

treck said:

I even tried TextWrangler and the same thing is happening.

Click to expand...

Same thing here -- TextWrangler is simply a text editor, it can't compile your code (and opening the file in Xcode gives you the same thing as above). As gekko said, Xcode can be very confusing and tricky to get set up, especially if you're trying to use pre-release APIs as you seem to be with Java 5.

Frankly, I would keep it really basic and simple at first and use TextWrangler to edit your source files and Terminal.app (javac) to compile them into your Java apps. It's good to know how it all works at a low level before you start using more advanced IDEs and build tools such as Ant, Eclipse, NetBeans, and Xcode.

Last night I opened up eclipse and went through the tutorial and it worked fine. I then open my .java file from class and it wouldn't compile. Before I fell asleep I remembered that I didn't bring the .class file with me. Would that have anything to do with my problem?

For beginning java, stick to any regular text editor, or google for some free text editors that have some basic java support (i.e. syntax coloring). Once you understand the basic stuff, try BlueJ; its got some of the advanced features like project management, but a lot of beginners use it. Once you get advanced, try out one of the IDEs. The ones I use are Eclipse (primary) and Xcode (secondary primary ).

I have my beefs with each of them though. Here are my main likes and dislikes of each:

Xcode Likes:
- Loads fast and is very responsive
- OS Integration
- Slick interface
- Good project management
- Project Symbols
- Data modeling (I've seen it but not tried it yet)
- Predictive Compilation
- CVS compatible (I should be setting up a CVS repository for my growing number of project very soon)

Eclipse Dislikes:
- (Very) Slow loading and very slow responsiveness
- Code completion popup is so slow to come up, I rarely use it anymore
- Workspace can easily look/feel cluttered or "messy"
- Too much baggage in general
- Preferences can be confusing to customize sometimes

I'd say Eclipse would be easier to move into out of the 2.

I'm thinking about making an IDE (in Java) thats a cross between Xcode and Eclipse, though its a lot of work for a single programmer (i.e. me), and with college around the corner, I doubt I could get a lot proper planning/work done on it. That's so far is the only reason why I haven't already started.

It was kind of simple if you think about it. I opened eclipse, created a new project, opened my .java from class (shows up in the work space as *book.java), I then renamed the file to match the project name (book.java)and the class was automatically created and I could then compile and run it. Thanks every one for your time and effort.

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.