Learning Java on OS X

googolplex

jeff.hume.ca
Hey,
I'm wanting to learn Java and possibly Objective-C on OS X. I'm wondering if Project and Interface Builder are the right tools to do so. Also I would like to get a good book on the subject. The learning cocoa book by oreilly looks good, but its not a book teaching you java or c just cocoa. If somebody could suggest a good book or website that would be great. Also If I write java code on os x will it work on other platforms using the Java VM.

Thanks,
googolplex
 
Hey dude. You can use those tools (Apples * Builders) to create Java programs, but they won't be portable w/o a lot of work!

The best suggestion is to go IDE or by hand. Netbeans is pretty neat, but I'm a beginner with Java and I've had a heck of a time installing that.

Borland has a product named JBuilder that has a demo that you can use until the rest of the year, and into January I think. It's pretty neat, but very much more than what I'm capable of right now.

I've had great luck with the Sam's Teach yourself in 21 day books (The one on C is Awsome), and the one for Java isn't as good, but pretty darn helpful (they need to explain the actual object oriented part better).

Starting out you'll be doing a lot of stuff that is used on a terminal. I say stick with VI or any standard text editor you want.

Any standard Java stuff you write will be very portable. There are some neat things you can do to help the app launch better for some platforms, but we'll both learn that once we get used to the langauge.

Hope this helps, or at least keeps you interested!

-Jim
 
If I write Java code without using Interface builder and project builder how can I get it to use the aqua gui elements? Is it possible? Is there a GUI library somewhere? I think I might get Netbeans... it looks interesting... I'll check out Jbuilder as well. I'm not sure if I want to spend much money on something know, however, as I just want to do this for fun.

googolplex
 
That's a wonderful question google!

The Swing interface was designed to go along with the AWT (abstraction window tool?) to make it a bit nicer on the eye.

Mac OS X has a wonderful way of making Swing fully native with Aqua. So ANY Java app will take advantage of Aqua and look nice and pretty. Just watch out for those Mac OS X specific elements in your interface :)

And, I was a little off with my post above. You can create a good old fashion Java application with Project Builder and have it cross platform.

I would look into the Java Cocoa implementation, the Interface design that way as opposed to the way Java does it is to die for!

But then again, you'll lose cross platform portability.

-Jim
 
Hey,
Thanks for your replies. If I use swing with an application does it look as nice as if I use Interface builder? I've noticed that some java apps on os x (limewire, etc.) don't look as nice as others. Is that because they use swing?
googolplex
 
Exactly my man.
The controls aren't exactly the same size, and Aqua adds some nice shadow drops which may not be as nice looking if you designed it on the mac knowing what it would look like. But. . . it's pretty close (but you'll never hear me say "close enough") :)

By using Interface builder and thereby using the Cocoa framework, you'll have a much more sane way of designing the UI. It will be perfect to Aqua spec.

-Jim
 
Hey,
I'm thinking that I may learn cocoa. I think its really cool. I'd like to try and make some stuff with project builder/interface builder because they look really cool. I was thinking about getting the Learning Cocoa book by Orielly. However, it seems to me that the book tells you how to apply Objective-C and Java with Cocoa. It seems that they assume you have previous knowlage of Objective-C and Java. I have minimal knowlage of Java, but not enough for me to really do anything. And I have absolutly no knowlage of Objective-C. How would you recommend I learn the basics before I go into Cocoa. Also what would you recommed I spend time learning - Obj-C or Java?

Thanks,
googolplex
 
I am just a guy learning many languages at the same time. But they all have one thing in common: c. The C langauge is a great stepping stone many languages (well, most but BASIC). But from everything I've read, we really don't have to know a whole lot.

C has a very basic approach to how to properly code. There isn't a whole lot of syntax to learn. Sure if you want to use libraries you'll need to know how to use them, but they are all very simple to follow: albiet long as all heck!

I am trying to focus on Java and Objective C at the moment. i'm taking a C++ coarse right now and it's allright. I hated it at first, but it's starting to grow on me. For college I'll spend two years on C and another two on Java. Which is the head start on Java and C. (There's less typing (in a sense) in C than c++, but involves a lot more thinking. c++ is a mindless language).

Safe bet: read the three PDF files that apple put in the dev tools install. I'm starting with the ObjC.pdf that has everything about the language. The only thing holding me back from being a massively active programmer and releasing oodles and oodles of stuff, is that I cannot grasp object oriented programming. I don't see the point yet. When I get the DUH! factor to come in and convince me that it's worth anything, it may get thigns started.

There really is all the information we need to know with the dev tools install. The only problem is that there's a lot of it. After that, hit the currency converter (the obj-c tutorial), and then hit the java temp converter one. You should be all set with the OOP aspects, and should have enough experience to figure out whihc syntax YOU prefer. If you're anything like me in learning this stuff, hands on practice and experience wins every time.

I've read many things about people having minimal C knowlege, and picking up obj-c very easily (or should I say, rapidly?).

Go out, have some fun with this stuff. For me, I don't really get a good enough grasp on my liking of a language until after a few months or so doing real world stuff with it. Java and Obj-C are both VERY attractice languages.

I hope this helps, or at least doesn't scare you away! I can do amazing things with Visual Basic, but I don't like bloated software!

-Jim
 
Alright,
Well, I think I'm going to try out looking at the pdfs. I took a quick look at the Java one, but I didn't look at it closely.
googolplex
 
Java under OS X, utilizing Swing, is beautiful, if the author has tweaked his calls to the javax.swing libraries ( basically, your have to remove most the liquidness of the default layouts, and place many-a-panel-within-a-panel-within-a-panel ).

Mac OS X, with Developer tools installed, includes, by default, one of the best installations of the JDK, with everything you could ever need.

Here's an experiment:

Fire up PB (Project Builder), and create a new empty file. Name the file Test.java.

Type in the following:

import javax.swing.*

Public Class Test
{
public void main ( String args[] )
{
Int iValue;
String sValue;

sValue = JOptionPane.showInputDialog(null, "Enter an integer:");
iValue = Integer.parseInt(sValue);
JOptionPane.showMessageDialog("You typed: " + iValue);

System.exit(0)
}

}

Save you document.

Fire up a Terminal window, and navigate to the directory where you saved the Test.java file (by default, it's the root level of your home directory).

Type, in the terminal:

javac Test.java

This will compile our little app.
Now type:

java Test.java

This will run the app.

Neat!
If you have a windows machine, with the JDE installed, copy the .class file over. Run it using the same commands.

Works perfectly.

If you wanted to, you could take the MRJ App builder, and create a double-clickable version of this for Mac OS X and Mac OS 9.

Java on OS X is slick, and in my experience, outpreforms the Windows version, through menus have some oddities that don't exist in Windows.

If you're interested, you can combine Java with the Application Kit of Cocoa, and create Java-Cocoa apps. I've only tried this in passing, so I don't have any off-the-cuff examples. You can also then use Interface Builder to construct your interface.

It seems Apple loves Java.
 
jackshedd,
I'd love to see how cool the java example you gave is, but it didn't work. When I typed in exactly what you said and tried compiling it it said:

Test.java:1: ';' expected
import javax.swing.*
^
1 error

So I though to myself ok it just wants a semicolon there, that makes sense, so I put that in. Then I saved and tried compiling again and I got this:

Test.java:3: 'class' or 'interface' expected
Public Class Test
^
1 error


So I thought ok, there is some weird syntax thing here that it wants. So I put in class at the beggining of the line because I figured this is makeing a class not an interface. So I saved and tried to compile and I got this:

Test.java:3: '{' expected
class Public Class Test
^
Test.java:17: '}' expected
}
^
2 errors

Well, now I've got two errors. Then I thought what it might be. There was something wrong with your syntax and it thinks I am declaring two classes - Public and Test So it wants me to put the coresponding curly brackets in. So I got rid of "class Public" and instead just put class Test. So i compiled it and got this:

Test.java:14: ';' expected
System.exit(0)
^
Test.java:7: cannot resolve symbol
symbol : class Int
location: class Test
Int iValue;
^
2 errors

OK. I can figure out the first error. Just annother syntax semicolon thing. I put in the semicolon. I have no idea what the second error means, however. So I took a guess and figured that it should be "int" instead of "Int" and so I got rid of that. So I compiled again and got this:

Test.java:12: cannot resolve symbol
symbol : method showMessageDialog (java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("You typed: " + iValue);
^
1 error

At this point I have absolutly no idea what the hell it is asking me to do! ;). If somebody could tell me what its saying I would appriciate that. BTW because this uses a proportional font and it is wrapping some of my text the "^"s in my error statements might not correspond with where they were actually placed in it by the terminal. In the last example I noticed this because it is wrapping the text to the next line. The "^" is actually suppose to go by the dot after JOptionPane.

Hmm I'd like to figure this out,
Jeff
 
Shit...I'm sorry.

I'm terrible at syntax.

Try it this:

import javax.swing.*;

Public Class Test
{
public static void main ( String args[] )
{
int iValue;
String sValue;

sValue = JOptionPane.showMessageDialog(null, "Enter an integer:");
iValue = Integer.parseInt(sValue);
JOptionPane.showMessageDialog(null, "You typed: " + iValue);

System.exit(0);
}
}
 
import javax.swing.*;

public class Calculate
{
public static void main( String args[] )
{
//create variables needed. We're just using ints and strings.
String strNum1,
strNum2,
strResults;

int intNum1,
intNum2,
intSum,
intProduct,
intDifference,
intQuotient;

//ask the user for input
strNum1 =
JOptionPane.showInputDialog ( "Enter first integer: ");
strNum2 =
JOptionPane.showInputDialog ( "Enter second integer: ");

//parse the strings returned into ints
intNum1 = Integer.parseInt ( strNum1 );
intNum2 = Integer.parseInt ( strNum2 );

//calculate
intSum = intNum1 + intNum2;
intProduct = intNum1 * intNum2;
intDifference = intNum1 - intNum2;

//initialize strResults and begin to build output
strResults = "Results were: \n";

//deal with the possibility the user entered a zero
if ( intNum1 != 0 )
{
if( intNum2 != 0 )
{
intQuotient = intNum1 / intNum2;
strResults = strResults + "The quotient is: " + intQuotient + ".\n";
}
else
{
strResults = strResults + "Quotient cannot be calculated. The second number was 0.\n";
}
}
else
{
strResults = strResults + "Quotient cannot be calculated. The first number was 0.\n";
}

//finish building the output
strResults = strResults + "The sum is: " + intSum + ".\n";
strResults = strResults + "The product is: " + intProduct + ".\n";
strResults = strResults + "The difference is: " + intDifference + ".\n";

//Output
JOptionPane.showMessageDialog(null, strResults, "Results", JOptionPane.INFORMATION_MESSAGE );

//end
System.exit(0);
}
}



Save the file as Calculate.java.
 
Cool,
I'll try those examples when I get home. I'm at school now on a windows machine. Thanks for the examples.
googolplex
 
Well,
Both examples didn't work again. In the first one I tried it and it gave me an error saying i should have class before Public. Then I figured it was a case problem and changed it to public class... Then I tried again and got this:

Test.java:10: incompatible types
found : void
required: java.lang.String
sValue = JOptionPane.showMessageDialog(null, "Enter an integer:");
^

So then since I didn't know what that meant I tried the next example.

I foudn that it compiled, because it gave me no error, but then i did java Calculate.java and it said:

Exception in thread "main" java.lang.NoClassDefFoundError: Calculate/java

And I dont know what that means. I'd love to see this cool thing your telling me, but I can't ;).
 
On the second one:

Type

javac Calculate.java
Then

java Calculate

I have no idea what's going on with the first example. it complied fine on my machine...

Odd.
 
In case you're wondering, there are a couple of other good IDEs for the heavy duty stuff that would need more features than a simple text editor (debugger, project management, etc.) First is BlueJ, which is actually written in Java. You can get it at http://bluej.monash.edu. It's what more than 90% of my CS class uses, with only mild complaints. There's also Apple's Project Builder which offers TONS more features than BlueJ (which is intended as a simple, mostly educational IDE.) Not only can it do java, but it can do just about every combination of c, c++, java, and cocoa that you can throw at it. If you take some time to poke around with its functions and settings, you might be pleasently surprised. I certainly was.
 
Be very careful about BlueJ, it behaves very odd on Mac OS X machines. It runs a VM by itself (hmmm... sharing, anyone?) and uses a lot of resources which it doesn't need (using 50% of the system resources when in the background is not good).

Further it will not dispose of any objects created (I'm not sure that they are disposed until you actually log out...) which means that it will eat your memory alive, and not spit it back out.

Save for this it's a nice tool for Java beginners, helps a lot with the understanding of object-oriented-ism's. It works better on Windows and Linux. You could probably make a better version of it if you took the Windows version and tweaked it a little yourself. But that's another thread.


theo
 
Yeah, those are some of the reasons why I don't use BlueJ on a regular basis (aside from the really crappy text editor.) So far, it seems that Project Builder and javac are the best combination for writing java programs. Now, if I can find an option to make Project Builder spit out plain old class and jar files instead of building an application...
 
Back
Top