• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compiling my first program on OS/X  RSS feed

 
Simos Ketsetsoglou
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear fellows, i am new in Java and in programming in general as well. I also bought O'reilly's Head First Java and i am also stuck at the beginning.

I work on a MacBook Pro which is running Mac OS X 10.6.8.

First of all i find difficult to understand the whole thing about adding an entry to my own path and so i haven't done anything about that, so i would really appreciate if you could elaborate a bit on that concept.
However after reading the above replies i have managed to write the very first source code file using TextWrangler and saved it in the user directory.
The code is that:

import java.awt.*;
import java.awt.event.*;
class Party {
public void buildInvite() {

Frame f = new Frame();
Label l = new Label("Party at Tim's");
Button b = new Button("You Bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(l);
} // more code here...
}

Now through the terminal i managed to compile it and a new file was created (Party.class). But when i try to run it by typing java Party i get the following error:

macbookpro-2:~ simosketsetsoglou$ java Party
Exception in thread "main" java.lang.NoSuchMethodError: main


Can you please assist.

Thank in advance for your help.

Simos

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you literally have this:

//more code here

or is that just your shorthand way of telling us that there is more code that you didn't put into your post here?

the error message is actually quite clear. You do not have a method named "main", so the jvm doesn't know how to start your program. I'm not sure where this code came from, but if what you posted is literally what you have in your file, my guess is that it is an example of what code looks like, but not a fully functioning, runable program.
 
Simos Ketsetsoglou
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well to tell the truth i am not really sure. The code i posted is exactly what the book tells the reader to type and if you follow the steps then it should work.

However there is a note at the bottom of the page that tells that "this is not meant to be a tutorial... you'll be writing real code in a moment, but for now we just want you to get a feel for how it al fits together".

So maybe the whole thing with the 4 steps, Source, Compiler, Output, Virtual machines, is for me to just get an idea of how this should work in the future and not actually type this exact code. Correct?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's my guess, yes. I think you are correct.

As to your PATH question...

OSX comes with java pre-installed. Windows/*nix does not. So, your system probably already had the PATH variable set before you ever turned it on. The PATH variable is used by an operating system that says "when someone says 'run this program', look here to find the actual executable". On windows, it is usually an .exe file. I'm not sure what they're called on OSX.
 
Simos Ketsetsoglou
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help.

One last question (for now i am sure...) It's about navigating through the directories in terminal. I have created a subfolder folder called "Java test code" in the folder macbookpro-2:~ simosketsetsoglou$. Now when i type cd Java test code i get the foll:

macbookpro-2:~ simosketsetsoglou$ ls
Desktop Music Shared
Desktop DB MyFirstApp.class Sites
Desktop DF MyFirstApp.java StuffIt
Documents NetBeansProjects TheVolumeSettingsFolder
Downloads Network Trash Folder Traktor3
Downloads alias Party.class lost
Incomplete Party.java plus
Java test code Pictures templatesmonster
Library Public timemachine_backup_003
Movies Send Registration
macbookpro-2:~ simosketsetsoglou$ cd Java test code
-bash: cd: Java: No such file or directory

Do you have any idea why does it not go to that folder?
 
Simos Ketsetsoglou
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i found it on my own. It needs a \ to determine that there is a space between the words of the filename. so is should be" cd Java\ test\ code
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to be working at the Unix command line, it's best to avoid file names with spaces. I tend to use - rather than spaces to separate words.
 
Simos Ketsetsoglou
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks i'll keep that in mind
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I suspect you got a (very old) version of the book.
Frame, Label, Button are all AWT which have been obsolete and not recommended since 1.2 The recommended alternatives are Swing versions, viz. JFrame, JLabel, JButton

I would suggest getting a more up to date version of the book
 
Simos Ketsetsoglou
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
I suspect you got a (very old) version of the book.
Frame, Label, Button are all AWT which have been obsolete and not recommended since 1.2 The recommended alternatives are Swing versions, viz. JFrame, JLabel, JButton

I would suggest getting a more up to date version of the book


Well the Book says on the front page that it covers Java 5.0. Is this old?
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OSX comes with java pre-installed. Windows/*nix does not.


A nitpick:

Some Linux (which is also *nix) versions come with pre-installed java.
The first piece of advice given to people seeking assistance in this situation is to ditch it
and to install the "real" (Sun/Oracle) one.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simos Ketsetsoglou wrote:
Well the Book says on the front page that it covers Java 5.0. Is this old?

Well looks like the old code samples haven't been updated. The current version is 7. So 5 is not that old.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!