File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Head First Java -- Need Help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java -- Need Help!" Watch "Head First Java -- Need Help!" New topic
Author

Head First Java -- Need Help!

JMH Harris
Greenhorn

Joined: Nov 12, 2006
Posts: 3
Hello All,
Ok, this is my first time on the Forum, with my first book on Java, and my first Java question. A lot of firsts!
On page 3 of �Head First Java� the authors have a short program for the newbie to demonstrate all the neat aspects of Java. There is one problem; I am unable to get this program to work. The source will compile per the instructions in the book into byte code. But, when the byte code is executed I receive the following error message:

Exception in thread �main� java.lang.NoSuchMethodError: main

I am pretty sure the JDK & JRE are installed properly and the path variable is set correctly because the MyFirstApp application works as described on page 9 of �Head First Java�. So what am I missing?? Also, how does one display in a readable format the *.class file? I would like to be able to see the bytecode.

My future in Java programming is in the balance here!

Kind Regards,
John

P.S. Here�s the code:


import java.awt.*;

import java.awt.event.*;

class Party {

public void buildInvite(){

Frame f = new Frame();
Label I = new Label("Party at Tim's");
Button b = new Button("You bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(I);
} //more code here...
}
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282

But, when the byte code is executed I receive the following error message:

Exception in thread �main� java.lang.NoSuchMethodError: main



I like your style of entering present fora, put your cards on the table so to speak.

I have been at this for several years, and am now going to the real world with a real program, something to show for all this effort:


Once and again, you will commit the 'cannot see the forest because all the trees are in the way !' method of making forward progress.

First off, don't be shy about posting code - I have tried out the waters and no one seems to mind it: All of us understand the need to see the code that balances the post may seem excessive to the beginner, but may contain the clue that the trained eye can spot scanning through it.

Your diagnostic states there is no method main() .... but your code does not reveal whether you have a main() about which I note some interesting properties:
  • A class may have a main method.
  • The java virtual machine will only call a main that looks a certian way.
  • public static void main(String[] args)
  • You could have another main, if the TYPES inside the parenthesis were different
  • You can put a main() in your program, even if you do not call it.
  • You can call main - in some class - from somewhere other than the JVM
  • main() is just the cannonical entry point, so at least one place to start is known by convention
  • That does not mean you HAVE TO start there, it's just a way to make sense of things.

  • I have seen the error cited more than once in my work - there are some more highly qualified people who will likely get you steered (?) right away. I fix as many as one hundred errors at time, a few of them will be a consequence of the JVM and related issues, not directly an error of your own code, but get used to it. The primary skill of program coding is what I call kb_5 ... along about keyboard number five, you figure out that tearing up $19.95 plastic keyboards in frustration does not expedite program code efficiency.


    "The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Welcome to JavaRanch!

    To summarize: In order to invoke a Java program from the command line, the class must contain a "main" method with the signature...

    public static void main(String[] args) {...}

    This is the command line "entry point." If the class does not contain this method, then you will get the error message you posted when you try to run it.


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by JMH Harris:
    ... Also, how does one display in a readable format the *.class file? I would like to be able to see the bytecode...

    If it were in a "readable format," it wouldn't be bytecode.

    Actually, there is a tool for this. See javap - The Java Class File Disassembler.
    JMH Harris
    Greenhorn

    Joined: Nov 12, 2006
    Posts: 3
    Thanks Nicholas & Marc for the suggestion to use "main"! Here's what the new code looks like:

    import java.awt.*;

    import java.awt.event.*;

    class Party {

    public static void main(String[] args){
    //public void buildInvite(){

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

    Guess what? Now the program runs without that previous "main" error! In fact I get no error messages at all! Great! But now the program just ends without producing that cute GUI with the two buttons. So are we saying the example on page 3 of "Head First Java" is in error for not including the "main" method and why am I not seeing the GUI so eloquently portrayed in the text?

    Kind Regards,
    John
    David McCombs
    Ranch Hand

    Joined: Oct 17, 2006
    Posts: 212
    IMO, Don't worry about it. It is not really important, and the code is, as you found out lacking a main, but is also missing other elements. Changing the name of the method to main isn't an answer as you found out. Sometimes HF gets too cute for its good. But if you look at the disclaimer at the bottom of that page, they sort of say this isn't meant to be working code.

    Move on, this code is really meant to show the basic steps in getting some code to run. IMO, writing code for a GUI is not something beginners should do. Only after a solid foundation in language basics and understanding basic OOP principles, should you try to learn GUI. There are a lot of concepts you need to understand in that snippet of code.

    HF ignores GUI's for the majority of the book, and probably for similar reasons. When they do tackle GUI's they do so in a weak way, and really is a seperate book anyway.

    As an aside, their convention of using a single character for names of variables is extremely poor practice. Use meaningful names.

    If you are really curious here is some code that does something similar, minus the fonts and nothing really works.



    Again, don't worry about the code on that page. Just understand the steps in running the program.


    "Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
    Nicholas Jordan
    Ranch Hand

    Joined: Sep 17, 2006
    Posts: 1282
    Originally posted by JMH Harris:
    ...Here's what the new code looks like:


    I throw away more code than I use,.....

    ...snip...But now the program just ends without producing that cute GUI with the two buttons.


    GUI's tend to be clumsy, one issue you will run into quickly is once you get a window, closing the window may close the window without closing out the JVM, the engine that put it up. Heads first will likely address this issue and give a workable solution within a few chapters, if not, someone here will give help on the matter.

    So are we saying the example on page 3 of "Head First Java" is in error for not including the "main" method


    Yes,.. unless the program was supposed to be an applet - highly likely from the wording of your question. In which case you should have run it with the applet viewer. No harm done, just keep coding, compiling and testing.

    and why am I not seeing the GUI so eloquently portrayed in the text?


    No

    Messers Weber & McCombs give good advice. McCombs' code is the book answer to your original inquiry, - Mr Weber gives advice more along the style I suggest .... H.F. and other books perinially show how to put up a GUI, for good reason: The coupling between a window and thinking the computer is actually doing something is remarkable for it's penetration into human thinking.

    The Masters tend to use what is called a cli - command line interface,.... which is what you would call a DOS box.

    Once you become sufficiently proficient to put a main in your program, you can use java to accomplish a tremendous amount, then learn how to put up a GUI from a running java program.

    I concur with McCombs' "Don't worry about it." and remaining except for the "Object Oriented". That is one of several programming approaches, it is powerful for it's ability to keep things organized in you mind and code,.... but will produce no code in and of it's self - it is a way of thinking, a problem approach, that provides workable solutions to perrenial project killers on team efforts.

    There is no shortage of staunch members of that approach, and I will pay the price for giving you this advice - but Object Oriented does not write code, you do.

    Until you can get a program to do something useful, anything you can understand is the programming approach to use:

    Use the code tag, it makes things perceptible by those who may be able to help you.



    That will likely compile and run, but it just zips off the screen.

    Compare this problem with writing fifty lines of code just to get a window up. Keep reading, keep coding, keep asking questions.
    [ December 09, 2006: Message edited by: Nicholas Jordan ]
    John Samaniego
    Greenhorn

    Joined: Nov 15, 2006
    Posts: 2
    Hey guys! I'm also new to this forum... also new to this Java Planet thing... I'm a previous C++ programmer at my last semester so I'm trying to adopt to the Java environment. I already send some shipments for the Head First Java Book and I'm kinda excited to read the book because you guys are talking to it a lot here.

    JMH Harris nice humor there... You got me laughing in front of my PC...

    Even C++ convention is to use int main(...)
    Actually while reading the posts I really got no idea that the problem was the main... I only realized it when I saw the second post...

    Anyway, I'm a new to Java and I really want to learn a lot! what I mean a lot is like a 300 cups of coffee a day! hehe... anyway... I'm really fond of games and they say that Java is good in creating games since the Objective of Java is the cross-platform thing... like it can run to any PC or any Computers or whatever you call it... I would really want to be an apprentice of someone(MASTER Only)... It'll take like a month for the books to arive so by then I could have read half or 1/4 of the book...

    If you guys have an information about Head First's E-book please inform me... I really want to be like you guys who knows Java already... I'm now reading Java:How to Program by Deitel... Is it a good book? I read the C++:How to Program and some of its chapters are very confusing. It's not the way I want it to read so I used Absolute C++ since I like the flow of the chapters...
    Nicholas Jordan
    Ranch Hand

    Joined: Sep 17, 2006
    Posts: 1282
    Originally posted by John Samaniego:

    The Objective of Java is the cross-platform thing.....It'll take like a month for the books to arive so by then I could have read half or 1/4 of the book...

    You will be better off to read 14 books, JAVA is an arena of accomplished World Class Masters, this is the place to look ~ But I doubt seriously any Master is going to take you by the nano and lead you through the Coffee Plantation.

    I just cross-coded several hundred lines of Artificial Intelligence from classical C/C++ style to JAVA, using the compiler to point out where principal changes could be made. It's not that I have it working yet, it's that the linguistics are so similar that accompishment in C/C++ almost directly gains headway in JAVA.

    JAVA is, I think, an interface, employing many run-time checks that Dr. Bjarne Stroustrup, designer and implementer the C++ programming language, chose to leave out because the original work was a tool, designed in garage-shop prototyping, to write operating systems, by bench technicians to get some work done without all the bureaucratics of budgeting.

    It (JAVA) has since evolved into a

    **->very<-**

    sophisticated programming tool.
    [ December 10, 2006: Message edited by: Nicholas Jordan ]
    Bert Bates
    author
    Sheriff

    Joined: Oct 14, 2002
    Posts: 8815
        
        5
    Hi Guys,

    This is kind of funny

    At the bottom of the page we say that this code isn't meant to be a tutorial - the code you're discussing is just a snippet from a bigger program. As the note on the page says, don't get all hung up on this, you'll be writing real code in just a few pages!

    Have fun!

    Bert


    Spot false dilemmas now, ask me how!
    (If you're not on the edge, you're taking up too much room.)
    John Samaniego
    Greenhorn

    Joined: Nov 15, 2006
    Posts: 2
    Wow that was really a good information I must say... Nicholas Jordan... your like a java god or maybe even a java god himself... Sorry but I just heard that Cross-Platform thing on some of the students at school... and also to my brother so anyway... I wish you guys can help me somehow to become like you guys... though I'm 19 and its like a long way to go before I can be trully good at Java...

    I even saw the article that a 12-yr old kid wants to learn C++ and I can't imagine to think that At that Age I'm just learning to mas*@b*te... Sheesh... I wish I could be that kiddo... But... I just have to rely on how many hours I have to spend on learning...

    I really want to read that Head First Java because my brother has been telling me for months now and he said that it's a good book for beginners who wants to learn Java(other than the Deitel book)... I saw the book on my brother and I'm kinda hooked unto it because it's not a boring book...

    well I cant contact my brother now so I cant borrow the book so I guess I just have to wait for the book and continue reading this deitel's book for Java...

    yeah I've noticed that I'll be working more on Classes... and I hope you guys can help me a lot... and I do believe you guys can... hehe...
    Nicholas Jordan
    Ranch Hand

    Joined: Sep 17, 2006
    Posts: 1282
    Originally posted by John Samaniego:
    .... like a java god .....

    Java Gods live on Krakatoa, east of Java

    I am not degreed, if there were not so many Ph.D's in here, I would sign my posts: Nicholas Jordan, R.D. - Road Dog, claiming as my field of expertise the practical or field operations.

    Originally posted by John Samaniego:
    Sorry but I just heard that Cross-Platform thing on some of the students at school...

    Cross Platform is a stated goal of JAVA. What you heard in school is correct.

    I wish you guys can help me somehow to become like you guys... though I'm 19 and its like a long way to go before I can be trully good at Java...

    There are no shortages of people here who will do just exactly that.


    I even saw the article that a 12-yr old kid wants to learn C++ and I can't imagine to think that At that Age I'm just learning to mas*@b*te... Sheesh... I wish I could be that kiddo... But... I just have to rely on how many hours I have to spend on learning...

    Do not say things like: mas*@b*te - there is an ample supply of those on the web. Ususally these are expressions of excitement.

    I really want to read that Head First Java because my brother has been telling me for months now and he said that it's a good book for beginners who wants to learn Java(other than the Deitel book)... I saw the book on my brother and I'm kinda hooked unto it because it's not a boring book...

    Deitel is an accomplished master, the criticisim you will hear about H.F. is common to all beginner books, as well as your own code.

    well I cannot contact my brother now so I cant borrow the book so I guess I just have to wait for the book and continue reading this deitel's book for Java...

    Go to any library. If you cannot find some book, as the reference librarian about inter-library loan.


    yeah I've noticed that I'll be working more on Classes... and I hope you guys can help me a lot... and I do believe you guys can.





    Use the smileys, not poor construction - this place is swarmin with accomplished masters. Mastery of wordplay is something you will not beat them at. Consider the phrase: "Run like a Gazelle." You, the human can make ready use of this phrase. Trying to get machines to do this is like trying to figure out "Fifteen Ways to Stack a Cat". That is wordplay from a master in the field. If you can figure it out, there are about ten busloads of Ph.D's who will buy you a cup of coffee if you tell them.

    Oh, I forgot: Please take the extra few seconds to read what it actually says (whatever that is you are reading at any given moment) - this advice is from Philip R. "Phil" Zimmermann Jr. one of the most widely known masters in the field.

    Those of us who have been at it awhile grow to know this like ducks know water.

    [ November 23, 2006: Message edited by: Nicholas Jordan ]
    [ December 03, 2006: Message edited by: Nicholas Jordan ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Head First Java -- Need Help!